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:
UPDATE table_name
SET column_name=value;
We are going to create a table and then perform update operations in it.
Python3
import sqlite3
connection = sqlite3.connect( 'my_database.db' )
connection.execute(
)
print ( "Ship table created successfully" )
connection.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 sqlite3
connection = sqlite3.connect( 'my_database.db' )
connection.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' )" )
cursor = connection.execute( "SELECT * from ship" )
print ( "before updation" )
for row in cursor:
print (row)
connection.execute( "UPDATE ship set ship_name='manoji'" )
print ( "After updation" )
cursor = connection.execute( "SELECT * from ship" )
for row in cursor:
print (row)
connection.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 sqlite3
connection = sqlite3.connect( 'my_database.db' )
connection.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' )" )
cursor = connection.execute( "SELECT * from ship" )
print ( "before updation of ship address" )
for row in cursor:
print (row)
connection.execute( "UPDATE ship set ship_destination='delhi'" )
print ( "After updation of ship address" )
cursor = connection.execute( "SELECT * from ship" )
for row in cursor:
print (row)
connection.close()
|
Output:
Last Updated :
28 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...