简体   繁体   English

如何将变量插入数据库表sqlite3-Python

[英]How to insert variable to database table sqlite3 - Python

Whenever i try to run the following code i get an error saying that theres no such column "title_data", im really confused because "TITLE" is the column not "title_data" 每当我尝试运行以下代码时,我都会收到一条错误消息,说没有此类列“ title_data”,我真的很困惑,因为“ TITLE”是该列而不是“ title_data”

def insertData(self):
    title_Data = self.edit_title.text()
    year_Data = self.edit_year.text()
    rating_Data = self.edit_rating.text()


    connection = sqlite3.connect('films.db')
    try:
        connection.execute("INSERT INTO FILMS (TITLE,YEAR,RATING) VALUES(title_Data,year_Data,rating_Data)")


    except sqlite3.IntegrityError:
        print("You have already stored this data")
    connection.commit()
    connection.close()

You're not passing your variables correctly. 您没有正确传递变量。 Instead of 代替

connection.execute("INSERT INTO FILMS (TITLE,YEAR,RATING) VALUES(title_Data,year_Data,rating_Data)")

You should use 你应该用

connection.execute("INSERT INTO FILMS (TITLE,YEAR,RATING) VALUES(?,?,?)", (title_Data,year_Data,rating_Data))

See the docs for execute() for more information. 有关更多信息,请参见execute()的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM