How to update data in Table of SQLite3 Database using Python

Search Data in Table in SQLite3 Database in Python



Update Data in in SQLite3 Database Table 

If you want to update many data then also you have to update one by one as updating more than one value is not possible as of now in SQLite3


Query to Search data in database


"update table_name set column_name = ? where searchable_column_name=?",(new value, searchable_column_value))

  1. Here set column_name is the name of the column in which you want to update the value.
  2. Searchable_column_name is nothing but the location of the row where you want to update data example the primary key value.
  3. The data matched with searchable_column_value_data, there we have to update the value of the mentioned column_name.

Below is the data what we have in our database with table_name as Employee_Data and Id, Name and Salary as column name


Now let's update Name for Id 103


# program

import sqlite3 as sql

conn = sql.connect(
"Employee_Details.db") # creating connection with database(created in current directory)
curs = conn.cursor() # creating cursor to database

curs.execute("update Employee_Data set Name = ? where Id=?",("Mandeep", 103))
print("Data updated successfully ")

conn.commit()
conn.close()

 #Output

Data updated successfully

so in above query, we tried to change the Name for Id 103 


New Data in database


If you forget to commit(), data will not reflect in Database, as it will not get saved.





Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India