Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

sql

sqlite

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)")
like image 702
Hamzah Akhtar Avatar asked Apr 04 '14 20:04

Hamzah Akhtar


2 Answers

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))
like image 156
Tazz Avatar answered Sep 30 '22 19:09

Tazz


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.

like image 23
Gyan Veda Avatar answered Sep 30 '22 21:09

Gyan Veda