【问题标题】:SQL query via Python returns different results than direct SQL query. Why?通过 Python 的 SQL 查询返回的结果与直接 SQL 查询不同。为什么?
【发布时间】:2020-10-17 23:48:39
【问题描述】:

我目前正在使用 html、flask、sqlite3 和 Python 开发一个网站。

在我的 python 代码中,我运行这个 SQL 查询:

profile_rows = db.execute("SELECT * FROM profile WHERE profile_id = :profile_id", profile_id=session["user_id"])

在烧瓶提要中,我可以看到它工作正常并正确插入了 id:

SELECT * FROM profile WHERE profile_id = 11

查询没有返回任何应有的行。它就像数据库中没有行一样。

如果我运行我的程序使用的代码:

SELECT * FROM profile WHERE profile_id = 11

通过直接在数据库上的“phpLiteAdmin v1.9.7.1”我得到正确的行(那些在数据库中并且具有 profile_id 11 的行)。

怎么会这样? 我直接将 python 使用的查询(从提要)复制到 phpLiteAdmin,所以它不能给出不同的结果。 但确实如此!!!。请帮忙。

如果有帮助:我知道查询不会返回一行,因为我尝试了以下操作:

1

if len(profile_rows) == 0:
            return render_template("frontpage.html", warning="there are no rows in the db!")

返回警告信息

2

if profile_rows[0]["job"] is None:
            return redirect("/update_profile")

返回:“IndexError:列表索引超出范围”

3

return render_template("frontpage.html", warning="row count: " + len(profile_rows))

返回警告“行数:0”

我的代码中的所有其他查询都可以正常工作。只是这个没有

复制示例: 蟒蛇:

import os
import sys

import hashlib, binascii, os
import time
from functools import wraps
from cs50 import SQL
from flask import Flask, flash, jsonify, redirect, render_template, request, session
from flask_session import Session
from tempfile import mkdtemp
from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError
from werkzeug.security import check_password_hash, generate_password_hash

db = SQL("sqlite:///yourdatabase.db")

@app.route("/profile")
def profile():
        profile_rows = db.execute"SELECT * FROM profile WHERE profile_id = :profile_id", profile_id="1")


        if len(profile_rows) == 0:
            return render_template("profile.html", warning="row count: " + len(profile_rows))

        return render_template("profile.html")

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
        <title>Profile</title>
    </head>
    <body>
        {% if warning != NULL %}
            <div class="alert alert-warning alert-dismissible fade show" role="alert">
                <strong>Warning!</strong> {{ warning }}
            </div>
        {% endif %}
    </body>
</html>

SQLite3:

CREATE TABLE 'profile' ('profile_id' integer PRIMARY KEY NOT NULL, 'title' varchar(150), 'text' varchar(15000), 'job' varchar(100), 'area' varchar(150))

INSERT INTO "profile" ("profile_id","title","text","job","area") VALUES ('1','Hello world,','test','job','workfield')

【问题讨论】:

  • @snakecharmerb 如果这就是你的意思,我不会直接插入之前。我在重新加载站点和数据库之前和之后尝试了代码。但它在任何时候都不起作用。 “刷新会话”是什么意思,我该怎么做?我想我从来没有听说过。
  • 您是否尝试过在查询中使用 qmark 样式的占位符而不是 :profile_id? docs.python.org/3/library/sqlite3.html#cursor-objects
  • @TerrySpotts 是的,很遗憾我也尝试过,但没有成功
  • @snakecharmerb 你确定冲洗适用于 sqlite3 吗?我已经尝试过了,但只得到了错误。我还添加了一个生殖示例。我希望这就足够了。
  • 我假设您直接使用 (flask-)sqlalachemy,但您使用的是 cs50,它不会公开会话。所以不,冲洗不起作用。

标签: python sql sqlite flask cs50


【解决方案1】:

感谢所有有用的 cmets。不知何故,问题自己解决了。当我今天启动 CS50 IDE 时,该程序运行良好,无需更改任何一行代码。我不知道问题是什么,但只要它不再发生,我就没事。祝大家有个美好的一天!!!

由于某种原因,我今天无法结束这个话题,所以如果有人知道这是怎么发生的,我会全力以赴:D

我将在接下来的几天内关闭它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2012-01-08
    • 2015-08-04
    相关资源
    最近更新 更多