How do i update values in an SQL database? SQLite/Python

36,693

Solution 1

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.

Solution 2

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))

Solution 3

I'm not into Python, but i think i can help, so

cur.execute("UPDATE ExampleTable SET age = 18 WHERE age = 17")

If i'm wrong, sorry then

Solution 4

with con:
    cur = con.cursor()
    cur.execute("UPDATE Table_Name SET Age='18' WHERE Age='17'")
Share:
36,693
Hamzah Akhtar
Author by

Hamzah Akhtar

Updated on July 09, 2022

Comments

  • Hamzah Akhtar
    Hamzah Akhtar almost 2 years

    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)")