Let’s say, you have a database and you want to update the value inside that database table. What will be your solution to it?
What should You do to update values in a table inside an SQLite database? In this python tutorial, I will quickly guide you on how to update values in SQLite database.
Update values in SQLite Database table in python
To Update the values inside a SQLite database table, use the conn.execute() function with a Update Statement in it. This is a two-step process, first, you have to build an update query and then use the conn.execute() function to execute the query in python.
Let’s say you have a SQLite database that has the following data in it. If you want to create a database in python and insert data in it click on the link and find out how to do that.
Anyway back to the point, we have a SQLite database with one table called Student have the following data in it.
Name Course Age 0 AlixaProDev CS 19 1 Alixawebdev BBa 21 2 AskALixa Software 22
What if we want to change the age of AlixaProDev to 22. He is grown old now and needs to update the age in the SQLite database table.
Check out the following code on how we update the age of the student with the name ‘AlixaProDev’.
import pandas as pd import sqlite3 conn = sqlite3.connect('mydb.sqlite') print('Age Before Update') b_df = pd.read_sql("select * from Student ",conn) print(b_df) conn.execute('UPDATE Student SET Age=22 WHERE Name=? ',( 'AlixaProDev',)) conn.commit() print('Age Update Update') a_df =pd.read_sql("select * from Student ",conn) print(a_df) conn.close()
Output of the code
Age Before Update Name Course Age 0 AlixaProDev CS 19.0 1 Alixawebdev BBa 21.0 2 AskALixa Software 22.0 Age Update Update Name Course Age 0 AlixaProDev CS 22.0 1 Alixawebdev BBa 21.0 2 AskALixa Software 22.0
You can clearly see how we update the age of ‘ALixaProDev’ using python. You can use the same code to update any type of data available in the SQLite database in python.
Summary and Conclusion
This was it about this tutorial. I hope you liked this short tutorial. Let me know what you think aobut it.
I am a software Engineer having 4+ Years of Experience in Building full-stack applications.