通过python的sql查询返回的结果与直接sql查询不同为什么?

ar5n3qh5  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(387)

我目前正在使用html、flask、sqlite3和python开发一个网站。
在python代码中,我运行以下sql查询:

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

在 flask 进料中,我可以看到它工作正常,正确插入id:
从profile\u id=11的profile中选择*
查询不会像应该的那样返回任何行。它的行为就像数据库中没有行一样。
如果我运行程序使用的代码:

SELECT * FROM profile WHERE profile_id = 11

通过“phpliteadminv1.9.7.1”直接在数据库中获得正确的行(那些在数据库中并且具有概要文件id 11的行)。
怎么会这样?我直接将python使用的查询(从feed)复制到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:列表索引超出范围”

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

返回警告“行计数:0”
对于我代码中的所有其他查询,这工作得很好。只是这个不行
示例:python:

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')
xienkqul

xienkqul1#

谢谢你的宝贵意见。不知何故,问题自行解决了。当我今天启动cs50ide时,程序运行得很好,没有改变一行代码。我不知道是什么问题,但只要它不再发生,这是我的罚款。祝大家今天愉快!!!
由于某种原因,我今天不能结束这个主题,所以如果有人知道这是怎么发生的,我洗耳恭听
我将在未来几天关闭这个

相关问题