当我使用正确的列名和正确的语句时,为什么我的SQL查询会不断出现错误?

发布于 2025-02-02 02:32:26 字数 730 浏览 1 评论 0原文

我正在尝试使用MySQL Python和Flask创建书籍数据库系统。我的书籍表中的3列是标题,作者和publish_year。

我有一个sql命令

"SELECT * FROM Books WHERE title=" + str(title)"

,每当我尝试用端点调用我的端点时,

http://127.0.0.1:5000/getbookfromtitle/Twilight

我会发现

 Unknown column 'Twilight' in 'where clause'

任何人是否知道会导致此错误的是什么? 我的完整烧瓶功能是

@app.route("/getbookfromtitle/<title>", methods=["GET"])
def getBookFromTitle(title):
    if request.method == "GET":
        sql = "SELECT * FROM Books WHERE title=" + str(title)
        mycursor.execute(sql)
        result = mycursor.fetchall()

        for x in result:
            print(x)
        return str(result)

I am trying to create a book Database system using MySQL Python and Flask. My 3 columns in my Books table are title, author, and publish_year.

I have a SQL Command that says

"SELECT * FROM Books WHERE title=" + str(title)"

And whenever I try to call my endpoint with

http://127.0.0.1:5000/getbookfromtitle/Twilight

I get the error

 Unknown column 'Twilight' in 'where clause'

Does anyone know what could be causing this error?
My full Flask function is

@app.route("/getbookfromtitle/<title>", methods=["GET"])
def getBookFromTitle(title):
    if request.method == "GET":
        sql = "SELECT * FROM Books WHERE title=" + str(title)
        mycursor.execute(sql)
        result = mycursor.fetchall()

        for x in result:
            print(x)
        return str(result)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2025-02-09 02:32:26

没关系,我想我已经弄清楚了。

在这样做之后,我必须这样做

mycursor.execute("SELECT * FROM Books WHERE title=%s", (title,))

,它出于某种原因而起作用。

Never mind I think I figured it out.

I have to do

mycursor.execute("SELECT * FROM Books WHERE title=%s", (title,))

After I did this, it worked for some reason.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文