Open In App

How to Update all the Values of a Specific Column of SQLite Table using Python ?

Improve
Improve
Like Article
Like
Save
Share
Report

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




# importing sqlite module
import sqlite3
  
# create connection to the database my_database
connection = sqlite3.connect('my_database.db')
  
# create table named address of customers 
# with 4 columns id,name age and address
connection.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 connection
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 sqlite module database
import sqlite3
  
# create connection to the database
# my_database
connection = sqlite3.connect('my_database.db')
  
# insert query to insert values
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' )")
  
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("before updation")
  
# display row by row
for row in cursor:
    print(row)
  
# query to update all data in ship_name 
# column to manoji
connection.execute("UPDATE ship set ship_name='manoji'")
  
print("After  updation")
  
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
    print(row)
  
# close the connection
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 sqlite module database
import sqlite3
  
# create connection to the database 
# my_database
connection = sqlite3.connect('my_database.db')
  
# insert query to insert values
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' )")
  
# query to display all data in the table
cursor = connection.execute("SELECT * from ship")
print("before updation of ship address")
  
# display row by row
for row in cursor:
    print(row)
  
# query to update all data in  ship_address
connection.execute("UPDATE ship set ship_destination='delhi'")
  
print("After  updation of ship address")
  
# display row by row
cursor = connection.execute("SELECT * from ship")
for row in cursor:
    print(row)
  
# close the connection
connection.close()


Output:



Last Updated : 28 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads