简体   繁体   中英

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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