问题:
Iam在搜索时有一个基于烧瓶的搜索栏,从MySQL获取数据并希望显示在同一页面上。
代码:
@app.route('/search', methods=['GET', 'POST'])
def search():
try:
if request.method == "POST":
conn = MySQLdb.connect(user="analyst", passwd="abc123", db="somedb", host="127.0.0.1")
cursor =conn.cursor()
cursor.execute("SELECT * from some where domain = '%s'"%(request.form['search']))
logger.info('Query is {}'.format("SELECT * from samples where md5 = '%s'"%(request.form['search'])))
data = cursor.fetchall()
logger.info('Data fetched is {}'.format(str(data)))
return render_template("search.htm", records=cursor.fetchall(), title='User',)
except Exception as e:
return render_template("search.htm", records=str(e), title='User',)
return render_template('search.htm',
title='Cuckoo User Page',
)
模板/ search.htm
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>{{ title }}</title>
<!-- Custom CSS -->
<link href="{{ url_for('static', filename='css/style.css') }}" />
<!-- Bootstrap -->
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>{{ title }} - Search Page</h1>
<form class="navbar-form navbar-left" action="{{ url_for('search') }}" method="POST" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" name="search" value="{{ request.form.search }}">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
{{ records }}
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
</body>
</html>
观察到的问题: 每当我搜索某些内容时,它都会从db中成功获取并将其存储在logs.But中,当它解析为html页面时,它显示为()
日志:
2017-09-08 11:28:14,098 - app.views - INFO - Query is SELECT * from some where domain = 'akizekij.bovseter.org '
2017-09-08 11:28:14,098 - app.views - INFO - Data fetched is (('a', '134', '11333', 'akizekij.bovseter.org', '10.10', '13', datetime.date(2017, 2, 28), datetime.date(2017, 2, 28), '', 0L, 1L),)
有关如何解决此问题的任何建议?
答案 0 :(得分:2)
你正在调用fetchall()
2次,这将使你返回空元组(或dict)。在记录第一次获取的手上 - 它正常显示。
快速修复应该如下所示。
data = cursor.fetchall()
logger.info('Data fetched is {}'.format(str(data)))
return render_template("search.htm", records=data, title='User',)
^^^^^^
修改强>
>>> import pymysql as mysql
>>> conn = mysql.connect(host='localhost', user='root', password='123456', database='demoapp')
<pymysql.connections.Connection object at 0x7f31e5973110>
>>> cur = conn.cursor()
>>> cur.execute("SELECT name FROM categories")
21
>>> d = cur.fetchall()
>>> d
(('Relationships',), ('Business',), ('Celebrity',), ('Events',), ('Fashion',), ('Law',), ('Fitness',), ('Medical',), ('MOVIES',), ('MUSIC',), ('Social',), ('Sport',), ('Tech',), ('Travel',), ('VIDEO',), ('Strains',), ('Food',), ('Education',), ('Activism',), ('Pets',), ('Headlines',))
>>> f = cur.fetchall()
>>> f
()
参考:https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/cursors.py
第295,74,43行