9

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)")
1
  • You do an UPDATE query, which should be covered by whatever tutorial you're using to learn SQL. Commented Apr 4, 2014 at 20:55

5 Answers 5

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))
Sign up to request clarification or add additional context in comments.

Comments

12

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.

Comments

2

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

Comments

1
with con:
    cur = con.cursor()
    cur.execute("UPDATE Table_Name SET Age='18' WHERE Age='17'")

Comments

-1

you must use update operation Instead of insert operation to change records.

the program would be as follows

cur=con.cursor()
com="update ExampleTable set age=1 where age=17"

try:
    cur.execute(com)

    con.commit()

except:

    print('error')
    con.rollback()

con.close()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.