I have created a table, and have inserted data into the table. I wanted to know how I could update/edit the data. For example, if I have multiple columns in the table, of which one is named 'age' and the data for the column is = '17', and I now wanted to replace '17' with '18', would I do the following?
import sqlite3 as lite
import sys
con = lite.connect('Records.db')
with con:
cur = con.cursor()
cur.execute("INSERT INTO ExampleTable(Age) VALUES(18) WHERE (Age = 17)")
In sqlite3 with Python3.x works to me something like this:
newPrice = '$19.99'
book_id = 4
cursor.execute('''UPDATE books SET price = ? WHERE id = ?''', (newPrice, book_id))
To update values in a SQL database using the SQLite library in Python, use a statement like this one.
cur.execute("UPDATE ExampleTable SET Age = 18 WHERE Age = 17")
For a great introduction to using SQLite in Python, see this tutorial.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With