How to Update all the Values of a Specific Column of SQLite Table using Python ?
In this article, we are going to update all the values of a specific column of a given SQLite table using Python. In order to update all the columns of a particular 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 the UPDATE statement as per our requirement.
Syntax:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
UPDATE table_name
SET column_name=value;
We are going to create a table and then perform update operations in it.
Python3
# importing sqlite moduleimport sqlite3 # create connection to the database my_databaseconnection = sqlite3.connect('my_database.db') # create table named address of customers # with 4 columns id,name age and addressconnection.execute('''CREATE TABLE ship (ship_id INT, ship_name \TEXT NOT NULL, ship_destination CHAR(50) NOT NULL); ''') print("Ship table created successfully") # close the connectionconnection.close() |
Output:
Ship table created successfully
Example 1:
Python program to insert records and perform update queries. Here we update all data in the ship_name column to manoji.
Python3
# import sqlite module databaseimport sqlite3 # create connection to the database# my_databaseconnection = sqlite3.connect('my_database.db') # insert query to insert valuesconnection.execute("INSERT INTO ship VALUES (1, 'tata-hitachi','noida' )")connection.execute("INSERT INTO ship VALUES (2, 'tata-mumbai','mumbai' )")connection.execute("INSERT INTO ship VALUES (3, 'tata-express','hyderabad' )") # query to display all data in the tablecursor = connection.execute("SELECT * from ship")print("before updation") # display row by rowfor row in cursor: print(row) # query to update all data in ship_name # column to manojiconnection.execute("UPDATE ship set ship_name='manoji'") print("After updation") # display row by rowcursor = connection.execute("SELECT * from ship")for row in cursor: print(row) # close the connectionconnection.close() |
Output:
Example 2:
In this program, we first insert data then update all data in ship_address to Delhi in the same table.
Python3
# import sqlite module databaseimport sqlite3 # create connection to the database # my_databaseconnection = sqlite3.connect('my_database.db') # insert query to insert valuesconnection.execute("INSERT INTO ship VALUES (1, 'tata-hitachi','noida' )")connection.execute("INSERT INTO ship VALUES (2, 'tata-mumbai','mumbai' )")connection.execute("INSERT INTO ship VALUES (3, 'tata-express','hyderabad' )") # query to display all data in the tablecursor = connection.execute("SELECT * from ship")print("before updation of ship address") # display row by rowfor row in cursor: print(row) # query to update all data in ship_addressconnection.execute("UPDATE ship set ship_destination='delhi'") print("After updation of ship address") # display row by rowcursor = connection.execute("SELECT * from ship")for row in cursor: print(row) # close the connectionconnection.close() |
Output:


