简述mini-web框架的增删改查

import  re,
# 定义空字典
URL_DICT=dict()
# 定义装饰器
def route(url):
	def set_func(func):
		URL_DICT[url] = func
		def call_func(*args,**kwargs):
			return func(*args,**kwargs)
		return call_func
	return set_func

@route('/index.html')
def index(ret):
	# 打开文件
	with open ('./templates/index.html') as f:
		content = f.read()
	# 创建connection链接
	conn = connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
	# 创建cursor游标对象
	cs = conn.cursor()
	# 执行sql语句
	sql = 'select * from info ;'
	cs.execute(sql)
	tuples = cs,fetchone()
	url_template = '''
	'''
	html = ''
	for info in tuples:
		html += url_template % (info[0].......)
	content = re.sub(r'\{%content%\}',html,content)
	return content

@route('/center.html')
def center(ret):
	with open ('./templates/center.html') as f:
		content = f.read()
	# 
@route('/add/(\d+)\.html')
def add_focus(ret):
	'''添加关注'''
	stock_code = ret.group(1)
	# 创建connect链接
	conn = connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
	# 创建cursor游标对象
	cs = conn.cursor()
	# 执行sql语句
	# 1.1 判断是否有这支股票
	sql = 'select * from info where code=%s'
	cs.execute(sql,(stock_code,)
	# 如果不存在这支股票
	if not cs.fetchone():
		cs.close()
		conn.close()
		return '这只股票不存在,大哥手下留情吧,我们是创业公司'
	# 1.2 判断股票是否关注
	sql = 'select * from info as i inner join focus as f on i.id=f.info_id where i.code = %s'
	cs.execute(sql,(stock_code,)
	# 如果股票已经关注
	if cs.fetchone():
		cs.close()
		conn.close()
		return '股票%s已经被关注,请勿重复关注'  % stock_code
	# 添加股票
	sql = 'insert into focus(info_id) select id from info as i where i.code=%s'
	cs.execute(sql,(stock_code,)
	conn.commit()
	cs.close()
	conn.close()
	return '添加成功'

@route('/del/(\d+)\.html')
def del_focus(ret):
	'''删除股票'''
	stock_code = ret.group(1)
	# 创建connect链接
	conn = connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
	# 创建cursor游标对象
	cs = conn.cursor()
	# 执行sql语句
	# 1.1 判断是否有这支股票
	sql = 'select * from info where code=%s'
	cs.execute(sql,(stock_code,)
	# 如果不存在这支股票
	if not cs.fetchone():
		cs.close()
		conn.close()
		return '这只股票不存在,大哥手下留情吧,我们是创业公司'
	# 1.2 判断股票是否关注
	sql = 'select * from info as i inner join focus as f on i.id=f.info_id where i.code = %s'
	cs.execute(sql,(stock_code,)
	# 如果股票已经关注
	if not cs.fetchone():
		cs.close()
		conn.close()
		return '股票%s还没有被关注,删除无效'  % stock_code
	# 删除股票
	sql = 'delete from focus as f inner join info as i on f.info_id=i.id where i.code=%s'
	cs.execute(sql,(stock_code,)
	conn.commit()
	cs.close()
	conn.close()
	return '删除成功'

@route('/update/(\d+)\.html')
def show_update_page(ret):
	'''显示修改的页面'''
	stock_code = ret.group(1)
	with open ('./templates/update.html') as f:
		content = f.read()
	# 创建connection链接
	conn = connect(host='localhost',port=3306,user='root',password='mysql',datdabase='stock_db',charset='utf8')
	# 创建cursor游标对象
	cs = conn.cursor()
	# 执行sql语句
	sql = 'select f.note_info from focus as f inner join info as i on f.info_id = i.id where i.code = %s'
	cs.execute(sql,(stock_code,)
	cs.close()
	conn.close()
	content = re.sub(r'\{%content%\}',stock_info[0],content)
	content = re.sub(r'\{%content%\}',stock_code,content)
	return content

@route('/update/(\d+)/(.*)\.html')
def save_update_focus(ret);
	'''保存修改页面'''
	stock_code = ret.group(1)
	stock_info = ret.group(2)
	comment = urllib.parse.unquote(stock_info)
	with open ('./templates/updata.html') as f:
		content = f.read()
	# 创建connection链接
	conn =connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
	# 创建cursor游标对象
	cs = conn.cursor()
	# 执行sql语句
	sql = 'update focus as f inner join info as i on f.info_id = i.id set f.note_info = %s where i.code=%s';
	cs.execute(sql,(comment,stock_code)
	conn.commit()
	cs.close()
	conn.close()
	return '修改成功'

def application(env,server_frame):
	'''WSGI接口'''
	staus = '200 OK'
	sponse_header = [('Content-Type','text/html;charset='utf-8')]
	file_name = env['PATH_INTO']
	try:
		# return URL-DICT[file_name]()
		for url,func in URL_DICT.items():
			ret = re.match(url,file_name)
			if ret:
				return func()
		else:
			return '没有该函数%s可执行' % file_name 
	except Exception as ret:
		return '产生异常%s' % str(ret)
	
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值