![](/img/trans.png)
[英]pyodbc & MS SQL Server - “No results. Previous SQL was not a query.”
[英]SQL query via Python returns different results than direct SQL query. Why?
我目前正在使用 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:
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”
與我的代碼中的所有其他查詢一起工作得很好。 只是這個沒有
繁殖示例: 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')
感謝您提供所有有用的評論。 不知何故,問題自己解決了。 當我今天啟動 CS50 IDE 時,該程序運行良好,無需更改一行代碼。 我不知道問題是什么,但只要它不再發生,我就沒事。 祝大家有個美好的一天!!!
由於某種原因,我今天無法結束這個話題,所以如果有人知道這是怎么發生的,我會全力以赴:D
我將在接下來的幾天內關閉它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.