Python 我查询我的sqlite数据库,结果为[](空白),即使我的数据库中有我试图查询的数据
我查询我的sqlite数据库,结果为[](空白),即使我的数据库中有我试图查询的数据 因此,我将Python文件与SQLite数据库连接(连接良好),并通过一个表单查询用户输入的数据,该表单通过在HTML文件上使用Jinja语法显示:Python 我查询我的sqlite数据库,结果为[](空白),即使我的数据库中有我试图查询的数据,python,database,sqlite,flask,Python,Database,Sqlite,Flask,我查询我的sqlite数据库,结果为[](空白),即使我的数据库中有我试图查询的数据 因此,我将Python文件与SQLite数据库连接(连接良好),并通过一个表单查询用户输入的数据,该表单通过在HTML文件上使用Jinja语法显示: import os and some other not important things MYDIR = os.path.dirname(__file__) SQLPATH = os.path.join(MYDIR, "..", "data.sqlite")
import os and some other not important things
MYDIR = os.path.dirname(__file__)
SQLPATH = os.path.join(MYDIR, "..", "data.sqlite")
conn = _sqlite3.connect(SQLPATH, check_same_thread=False)
c = conn.cursor()
core = Blueprint('core', __name__)
@core.route('/', methods=['GET', 'POST'])
def index():
# Call a function to later use in creating the template
search = Blogsearch_form(request.form)
print(search.data)
if request.method == 'POST':
c.execute("SELECT * FROM blog_post WHERE problem_name LIKE(?)", ('%' + str(search.data) + '%',))
results = c.fetchall()
print(results)
return render_template('blog_search_result.html', results=results)
# return blog_search_results(search)
page = request.args.get('page',1,type=int)
many_posts = BlogPost.query.order_by(BlogPost.date.desc()).paginate(page=page, per_page=10)
return render_template('index.html', many_posts=many_posts, form=search)
<form class="form-inline my-2 my-lg-0 text-right" method=POST>
{{ render_field(form.search) }}
<input type=submit value=Search>
</form>
您可以看到,我在代码中放置了两个打印以进行测试,下面是这些打印的结果:
打印(search.data):{'search':'newlife 3 check'}(newlife 3 check是我在HTML表单中键入的输入)
打印(结果):[]
下面是我的博客搜索表单的python文件:
from wtforms import Form, StringField
class Blogsearch_form(Form):
search = StringField('')
在我的HTML文件中显示表单的部分:
import os and some other not important things
MYDIR = os.path.dirname(__file__)
SQLPATH = os.path.join(MYDIR, "..", "data.sqlite")
conn = _sqlite3.connect(SQLPATH, check_same_thread=False)
c = conn.cursor()
core = Blueprint('core', __name__)
@core.route('/', methods=['GET', 'POST'])
def index():
# Call a function to later use in creating the template
search = Blogsearch_form(request.form)
print(search.data)
if request.method == 'POST':
c.execute("SELECT * FROM blog_post WHERE problem_name LIKE(?)", ('%' + str(search.data) + '%',))
results = c.fetchall()
print(results)
return render_template('blog_search_result.html', results=results)
# return blog_search_results(search)
page = request.args.get('page',1,type=int)
many_posts = BlogPost.query.order_by(BlogPost.date.desc()).paginate(page=page, per_page=10)
return render_template('index.html', many_posts=many_posts, form=search)
<form class="form-inline my-2 my-lg-0 text-right" method=POST>
{{ render_field(form.search) }}
<input type=submit value=Search>
</form>
{{render_field(form.search)}
结果应显示列prolem_名称所在行中包含“New Life 3 check”字样的所有数据。此行位于数据库“data.sqlite”中名为“blog_post”的表中。然而,正如我上面所展示的,它给出了一个空白[]。我坚持了一段时间,如果你能帮助我,我将不胜感激
谢谢大家! 我认为这条线应该是
c.execute("SELECT * FROM blog_post WHERE problem_name LIKE(?)", ('%' + str(search.search.data) + '%',))
而不是
c.execute("SELECT * FROM blog_post WHERE problem_name LIKE(?)", ('%' + str(search.data) + '%',))
因为您试图获取字段的数据,而不是整个表单的数据。我还建议您提供更可读的变量名称 成功了!!!!!是的,成功了,谢谢!!!我将重命名搜索变量:))嗨,Kartikeya,我有另一个关于python和sqlite的问题,我希望你也能在这方面帮助我:谢谢!