You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask中SQLite3查询返回空列表而非预期数据的问题求助

Flask中SQLite3查询返回空列表而非预期数据的问题求助

大家好,我最近在做一个Flask+SQLite3的小项目,遇到了一个棘手的问题:明明数据库里有对应的数据,但查询后传到HTML页面的结果始终是空列表,想请大家帮忙排查一下问题所在。

先贴出我的app.py代码:

from flask import Flask, render_template, request, g

import sys
import sqlite3
import os

current_directory = os.path.dirname(os.path.abspath(__file__))

app = Flask(__name__)

@app.route('/student')
def student():
    connection = sqlite3.connect(current_directory + "/sss.db")
    cursor = connection.cursor()
    Lastname='Volley'
    Firstname='Vlad'
    result = cursor.execute("SELECT * FROM Students WHERE Firstname=? AND Lastname=?", (Firstname,Lastname))
    result.fetchone()
    res = list(result)
    connection.commit()
    connection.close()
    return render_template('student.html', res=res)

if __name__ == "__main__":
    app.debug=True
    app.run(port=3000)

为了直观确认数据是否被正确获取,我在student.html里直接输出这个列表,代码片段如下:

<div>
        <div class="container mr-t">
          <h1>Is this You?</h1>
          <div class="container">
              <p>{{res}}</p>
          </div>
          <form id="emailForm">
              <button type="submit">Find Email and Password</button>
              <button style="background-color: red;" type="submit">Clear</button>
          </form>
          <p id="result"></p>
        </div>
</div>

目前的情况是:页面上总是显示[]空列表,但我已经确认SQLite3数据库的Students表中,确实存在Firstname='Vlad'且Lastname='Volley'的学生数据。我排查过数据库连接路径,应该是没问题的,实在搞不懂为什么拿不到数据,麻烦大家帮我看看问题出在哪,谢谢!


备注:内容来源于stack exchange,提问作者Theodore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 14:18:07