hi, how are you today
Till now we discussed
How to create database in sqlite3 database engine in Python
How to create table in database in sqlite3 database engine in Python.
How to insert data in database in sqlite3 database engine in Python
How to read/search data from database in sqlite3 database engine in Python
So today, taking one more step further, we will discuss how to update data in table inside database of SQLite3 database engine.
We are continuing with the same example of student database that we have discussed in previous post of creating table and inserting data.
Updating Data in database
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 till now in SQLite3
As explained earlier, curs is optional not mandatory while creating table and inserting data but mandatory for reading, updating and deleting data.
Syntax/Query to read all data from database
Here set column_name is the name of the column in which you want to update the value.
Searchable_column_name is nothing but the location of the row where you want to update data example the primary key value( employee id , bank account).
The data matched with searchable_column_value_data, there we have to update the value oft he mentioned column_name.
Below example will give you more clarity about searchable_data
If you want to update more than one data than again you have to use the update query as explained below
Updating more than one Data
Do not forget to save the data if you do any type of modification in table of database, i.e. inserting,updating and deleting. And to save use
Till now we discussed
How to create database in sqlite3 database engine in Python
How to create table in database in sqlite3 database engine in Python.
How to insert data in database in sqlite3 database engine in Python
How to read/search data from database in sqlite3 database engine in Python
So today, taking one more step further, we will discuss how to update data in table inside database of SQLite3 database engine.
We are continuing with the same example of student database that we have discussed in previous post of creating table and inserting data.
Updating Data in database
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 till now in SQLite3
As explained earlier, curs is optional not mandatory while creating table and inserting data but mandatory for reading, updating and deleting data.
Syntax/Query to read all data from database
curs.execute("update table_name set column_name = ? where searchable_column_name=?",(new value, searchable_column_value))
Here set column_name is the name of the column in which you want to update the value.
Searchable_column_name is nothing but the location of the row where you want to update data example the primary key value( employee id , bank account).
The data matched with searchable_column_value_data, there we have to update the value oft he mentioned column_name.
Below example will give you more clarity about searchable_data
combine code for how to read all data from database
Updating single Data
1: import sqlite3 as sql
2: s_id=int(input("enter student id: "))
3: s_sch=float(input("enter new scholarship"))
4: conn=sql.connect("mydatabase.db")
5: curs=conn.cursor()
6: curs.execute("update St_data set Student_scholarship = ? where Student_id=?",(s_sch, s_id))
7: conn.commit()
8: conn.close()
9: print("updated")
So in above example we learnt how to update data.If you want to update more than one data than again you have to use the update query as explained below
Updating more than one Data
1: import sqlite3 as sql
2: s_id=int(input("enter student id: "))
3: s_sch=float(input("enter new scholarship"))
4: s-class=int(input("enter student new class: "))
5: conn=sql.connect("mydatabase.db")
6: curs=conn.cursor()
7: curs.execute("update St_data set Student_scholarship = ? where Student_id=?",(s_sch, s_id))
8: curs.execute("update St_data set Student_class = ? where Student_id=?",(s_class, s_id))
9: conn.commit()
10: conn.close()
11: print("updated")
Do not forget to save the data if you do any type of modification in table of database, i.e. inserting,updating and deleting. And to save use
conn.commit()
Always remember to close the connection before closing or quitting your software
conn.close()
Hope you all like this post and I wish this proves useful to you.
Keep sharing & give your views about post in comments.
Comments are always welcomed for better improvement.
Keep sharing & give your views about post in comments.
Comments are always welcomed for better improvement.
Thanks :)
Comments
Post a Comment