前端与数据库的拼接

本文介绍了如何在不依赖框架的情况下,手动实现前端代码与数据库数据的拼接。主要步骤包括:从前端获取代码,从数据库中查询并获取数据,使用正则表达式将数据库内容插入前端代码,最后返回拼接后的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端代码与数据库的拼接

步骤
  • 1、拿到前端的代码
  • 2、拿到数据库的数据(用到数据库的查询,和数据的获取)
  • 3、进行拼接,将数据库的内容替换到前端(使用正则)
  • 4、返回拼接的内容
    第一步
    在这里插入图片描述
    在这里插入图片描述
    第二步
    在这里插入图片描述
    第三步和第四步
    在这里插入图片描述

在这里插入图片描述

这些是框架之外的工作,代码如下:

  • mini_web
# 根据不同的地址返回不同的响应体
import re
from pymysql import connect

# 创建一个空的字典
url_dict = dict()

def route(url):
	def set_fun(func):
		def call_fun(*args, **kwargs):
			print("添加权限")
			return func(*args, **kwargs)
		print("装饰后的地址:", call_fun)
		print("装饰后的请求地址:", url)
		# 装饰后自动添加到对应的字典中
		url_dict[url] = call_fun
		return call_fun
	return set_fun


def application(file_path):
	# 响应行
	response_line = "HTTP/1.1 200 OK\r\n"
	# 响应头
	response_head = "content-type:text/html;charset=utf-8\r\n"
	try:		
		# 根据不同的地址返回不同的响应体
		method = url_dict[file_path]  # 得到相应的函数
		response_body = method()  # 函数执行了
	except Exception as e:		
		# 网页找不到
		# 响应行
		response_line = "HTTP/1.1 404 NOT FOUND\r\n"
		# 响应体
		response_body = "not page is show!"
	return response_line, response_head, response_body

############################上面是框架##################################
# 路由的作用是用来上网的
# 上网最早就是打开网页
# 一个页面一个功能
# 展示首页
@route("/index.html")
def index():
	# 1. 展示前端的界面
	with open("./templates/index.html", 'r') as f:
		content = f.read()
	# 一行的数据html代码
	row_str = """
	<tr>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
        <td>%s</td>
        <td>
            <input type="button" value="添加" id="toAdd" name="toAdd" systemidvaule="000007">
        </td>
    </tr>
	"""

	# 2.获取数据库的数据
	# 1. 从数据库得到数据
	# 1.1连接数据库
	# 创建Connection连接
	conn = connect(host='localhost', port=3306, database='stock_db', user='root', password='mysql', charset='utf8')
	# 获得Cursor对象
	cs1 = conn.cursor()
	# 1.2 执行查询的sql语句
	cs1.execute("select * from info;")
	# 得到数据库的数据
	data = cs1.fetchall()
	# 1.3 关闭
	cs1.close()
	conn.close()

	# 数据库有多少数据,我们拼接多少条
	# 创建一个新的字符串
	table_str = ""
	for temp in data:
		table_str += row_str % (temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7])
	# 使用正则替换
	content_new = re.sub("\{%content%\}", table_str, content)
	return content_new

##################以下是center网页####################
@route("/center.html")
def center():
	# 1.拿到前端代码
	# 2. 拿到数据库代码
	# 3. 进行拼接
	# 4. 返回拼接的内容
	
	# 1. 拿到前端的代码
	with open("./templates/center.html", 'r') as f:
		content = f.read()

	# 2. 拿数据库的数据
	# 1. 从数据库得到数据
	# 1.1连接数据库
	# 创建Connection连接
	conn = connect(host='localhost', port=3306, database='stock_db', user='root', password='mysql', charset='utf8')
	# 获得Cursor对象
	cs1 = conn.cursor()
	# 1.2 执行查询的sql语句
	cs1.execute(
		"select info.code,info.short,info.chg,info.turnover,info.price,info.highs,focus.note_info from info inner join focus on focus.info_id = info.id;")
	# 得到数据库的数据
	data = cs1.fetchall()

	# 1.3 关闭
	cs1.close()
	conn.close()

	# 3. 替换
	# 一行的数据字符串
	row_str = """
	<tr>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>%s</td>
            <td>
                <a type="button" class="btn btn-default btn-xs" href="/update/300268.html"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> 修改 </a>
            </td>
            <td>
                <input type="button" value="删除" id="toDel" name="toDel" systemidvaule="300268">
            </td>
        </tr>
	"""

	# 数据库有多少 行我应该拼接多少 行
	table_str = ""
	for temp in data:
		table_str += row_str % (temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6])

	content_new = re.sub('\{%content%\}', table_str, content)

	# 4. 返回响应体
	return content_new

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值