Python SQLite – Update Specific Column

28 Apr, 2021
In this article, we will discuss how to update a specific column of a table in SQLite using Python.
In order to update a particular column in a table in SQL, we use the UPDATE query. The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using UPDATE statement as per our requirement.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2...., column n = value n
WHERE [condition];
Below are some examples which depict how to use UPDATE query in SQLite using Python.
Example 1: We are going to create a STUDENT table and then perform update operations in it by updating the SNAME of a student whose SID is 1 in the table.
Python3
# importing sqlite3 moduleimport sqlite3  # create connection by using object to # connect with gfg databaseconnection = sqlite3.connect('gfg.db')  # query to create a table named STUDENTconnection.execute(''' CREATE TABLE STUDENTS         (SID INT PRIMARY KEY     NOT NULL,         SNAME           TEXT    NOT NULL,         SAGE            INT     NOT NULL,         ADDRESS        CHAR(50));         ''')  # insert query to insert student details # in the above tableconnection.execute(    "INSERT INTO STUDENTS VALUES (1, 'mohan pavan', 22, 'ponnur' )")  connection.execute(    "INSERT INTO STUDENTS VALUES (2, 'sudheer', 28, 'chebrolu' )")  connection.execute(  "INSERT INTO STUDENTS VALUES (3, 'mohan', 22, 'tenali' )")  # creating cursor object to display all # the data in the tablecursor = connection.execute("SELECT * from STUDENTS")  # display dataprint('\nOriginal Table:')for row in cursor:    print(row)  # update query to update sname to sravan # where id = 1connection.execute("UPDATE STUDENTS set SNAME = 'sravan' where SID = 1")  # save the changesconnection.commit()  # creating cursor object to display all# the data in the tablecursor = connection.execute("SELECT * from STUDENTS")  # display dataprint('\nUpdated Table:')for row in cursor:    print(row)
Output:
Example 2:
Here is another program where we update the ADDRESS of all the rows whose SAGE column value is 22 in the same table.
Python3
# importing sqlite3 moduleimport sqlite3  # create connection by using object # to connect with gfg databaseconnection = sqlite3.connect('gfg.db')    # insert query to insert student details# in the above tableconnection.execute(    "INSERT INTO STUDENTS VALUES (5, 'mohan pavan', 22, 'ponnur' )")  connection.execute(    "INSERT INTO STUDENTS VALUES (6, 'sudheer', 28, 'chebrolu' )")  connection.execute(  "INSERT INTO STUDENTS VALUES (7, 'mohan', 22, 'tenali' )")  # creating cursor object to display all# the data in the tablecursor = connection.execute("SELECT * from STUDENTS")  # display dataprint('\nOriginal Table:')for row in cursor:    print(row)  # update query to update ADDRESSconnection.execute("UPDATE STUDENTS set ADDRESS = 'naga' where SAGE = 22")  # save the changesconnection.commit()  # creating cursor object to display # all the data in the tablecursor = connection.execute("SELECT * from STUDENTS")  # display dataprint('\nUpdated Table:')for row in cursor:    print(row)
Output:
Picked
Python-SQLite
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts