English 中文(简体)
Python Data Access Tutorial

Selected Reading

Python SQLite - Update Table
  • 时间:2024-12-22

Python SQLite - Update Table


Previous Page Next Page  

UPDATE Operation on any database imppes modifying the values of one or more records of a table, which are already available in the database. You can update the values of existing records in SQLite using the UPDATE statement.

To update specific rows, you need to use the WHERE clause along with it.

Syntax

Following is the syntax of the UPDATE statement in SQLite −


UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

Example

Assume we have created a table with name CRICKETERS using the following query −


sqpte> CREATE TABLE CRICKETERS (
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Age int,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255)
);
sqpte>

And if we have inserted 5 records in to it using INSERT statements as −


sqpte> insert into CRICKETERS values( Shikhar ,  Dhawan , 33,  Delhi ,  India );
sqpte> insert into CRICKETERS values( Jonathan ,  Trott , 38,  CapeTown ,  SouthAfrica );
sqpte> insert into CRICKETERS values( Kumara ,  Sangakkara , 41,  Matale ,  Srilanka );
sqpte> insert into CRICKETERS values( Virat ,  Kohp , 30,  Delhi ,  India );
sqpte> insert into CRICKETERS values( Rohit ,  Sharma , 32,  Nagpur ,  India );
sqpte>

Following Statement modifies the age of the cricketer, whose first name is Shikhar


sqpte> UPDATE CRICKETERS SET AGE = 45 WHERE FIRST_NAME =  Shikhar  ;
sqpte>

If you retrieve the record whose FIRST_NAME is Shikhar you observe that the age value has been changed to 45 −


sqpte> SELECT * FROM CRICKETERS WHERE FIRST_NAME =  Shikhar ;
First_Name Last_Name  Age Place_Of_B  Country
---------- ---------- ---- ---------- -------------
Shikhar    Dhawan     45   Delhi      India
sqpte>

If you haven’t used the WHERE clause values of all the records will be updated. Following UPDATE statement increases the age of all the records in the CRICKETERS table by 1 −


sqpte> UPDATE CRICKETERS SET AGE = AGE+1;
sqpte>

If you retrieve the contents of the table using SELECT command, you can see the updated values as −


sqpte> SELECT * FROM CRICKETERS;
First_Name Last_Name  Age  Place_Of_B Country
---------- ---------- ---- ---------- -------------
Shikhar    Dhawan     46   Delhi      India
Jonathan   Trott      39   CapeTown   SouthAfrica
Kumara     Sangakkara 42   Matale     Srilanka
Virat      Kohp      31   Delhi      India
Rohit      Sharma     33   Nagpur     India
sqpte>

Updating existing records using python

To add records to an existing table in SQLite database −

    Import sqpte3 package.

    Create a connection object using the connect() method by passing the name of the database as a parameter to it.

    The cursor() method returns a cursor object using which you can communicate with SQLite3 . Create a cursor object by invoking the cursor() object on the (above created) Connection object.

    Then, invoke the execute() method on the cursor object, by passing an UPDATE statement as a parameter to it.

Example

Following Python example, creates a table with name EMPLOYEE, inserts 5 records into it and, increases the age of all the male employees by 1 −


import sqpte3

#Connecting to sqpte
conn = sqpte3.connect( example.db )

#Creating a cursor object using the cursor() method
cursor = conn.cursor()

#Doping EMPLOYEE table if already exists.
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")

#Creating table as per requirement
sql =   CREATE TABLE EMPLOYEE(
   FIRST_NAME CHAR(20) NOT NULL,
   LAST_NAME CHAR(20),
   AGE INT,
   SEX CHAR(1),
   INCOME FLOAT
)   
cursor.execute(sql)

#Inserting data
cursor.execute(   INSERT INTO EMPLOYEE
   (FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES 
   ( Ramya ,  Rama priya , 27,  F , 9000),
   ( Vinay ,  Battacharya , 20,  M , 6000), 
   ( Sharukh ,  Sheik , 25,  M , 8300), 
   ( Sarmista ,  Sharma , 26,  F , 10000),
   ( Tripthi ,  Mishra , 24,  F , 6000)   )
conn.commit()

#Fetching all the rows before the update
print("Contents of the Employee table: ")
cursor.execute(   SELECT * from EMPLOYEE   )
print(cursor.fetchall())

#Updating the records
sql =    UPDATE EMPLOYEE SET AGE=AGE+1 WHERE SEX =  M     
cursor.execute(sql)
print("Table updated...... ")

#Fetching all the rows after the update
print("Contents of the Employee table after the update operation: ")
cursor.execute(   SELECT * from EMPLOYEE   )
print(cursor.fetchall())

#Commit your changes in the database
conn.commit()

#Closing the connection
conn.close()

Output


Contents of the Employee table:
[( Ramya ,  Rama priya , 27,  F , 9000.0), 
   ( Vinay ,  Battacharya , 20,  M , 6000.0), 
   ( Sharukh ,  Sheik , 25,  M , 8300.0), 
   ( Sarmista ,  Sharma , 26,  F , 10000.0), 
   ( Tripthi ,  Mishra , 24,  F , 6000.0)]
Table updated......
Contents of the Employee table after the update operation:
[( Ramya ,  Rama priya , 27,  F , 9000.0), 
   ( Vinay ,  Battacharya , 21,  M , 6000.0), 
   ( Sharukh ,  Sheik , 26,  M , 8300.0), 
   ( Sarmista ,  Sharma , 26,  F , 10000.0), 
   ( Tripthi ,  Mishra , 24,  F , 6000.0)]
Advertisements