hi, how are you today
Here sqlite3 is imported with the alias name as sql, so further action will be done with the alias name only
Step 2: Create database and get connection
Database name must be given with .db extension by programmer.
Step 3: Create cursor object
Step 4: Create table
Step 5: Operations
1)Insertion of data
2)Read/search data in database
It alwars reads the data as a " list of tuple(s)" or we can say "tuple(s) inside list"
3)Delete data from database
4)Update data in database
Step 6: Saving Data
we must save the modification to reflect the same in database table
Step 7: Closing Connection
So here we have seen all the syntax/query to perform various operations on sqlite3 database engine.
In this tutorial, I am simply giving you Syntax/query for all operations that can be done on SQLite3 database engine in python step by step.
I earlier tutorials we have discussed individually each and every topic. click here to take overview
I earlier tutorials we have discussed individually each and every topic. click here to take overview
Step 1: Import sqlite3
import sqlite3 as sql
As sqlite3 is inbuilt in python so we have to directly import it.Here sqlite3 is imported with the alias name as sql, so further action will be done with the alias name only
Step 2: Create database and get connection
conn=sql.connect("database_name.db")
This will give us connection object if database is already present. Anyhow if database doesn't exist, connect() will first create database and then return connection object.Database name must be given with .db extension by programmer.
Step 3: Create cursor object
curs=conn.cursor()
This will give us the cursor object that will be used to perform operations on database. Curs is mandatory to perform operation on database such as search, update and delete whereas optional in case of creation table and insertion of data.Step 4: Create table
curs=conn.execute("create table if not exists table_name(col_name type_of_data primary key(optional),col_name type_of_data,...)")
This will give us the cursor object that will be used to perform operations on database. Curs is mandatory to perform operation on database such as search, update and delete whereas optional in case of creation table and insertion of data.Step 5: Operations
1)Insertion of data
curs.execute("insert into table_name values(?,?,...)",(data1,data2,...))
no of "?" must be equal to the no of column in the table followed by same no of data collected in a tuple2)Read/search data in database
It alwars reads the data as a " list of tuple(s)" or we can say "tuple(s) inside list"
- search/read all
curs.execute("select * from table_name)
res = fetchall()
- search/read one
curs.execute("select * from table_name where searchable_column_name = ?",(searchable_column_value,))
res = fetchone()
- search/read many
curs.execute("select * from table_name)
res = fetchmany(count to fetch data)
3)Delete data from database
curs.execute("delete from table_name where searchable_column_name=?",(searchable_column_name_value))
4)Update data in database
curs.execute("update table_name set column_name = ? where searchable_column_name=?",(new value, searchable_column_value))
Step 6: Saving Data
conn.commit()
Saving the data is mandatory if any modification done on data present in database such as- insertion of new data
- updation of present data
- deletion of data from database
we must save the modification to reflect the same in database table
Step 7: Closing Connection
conn.close()
Closing the connection is mandatory and is done just before the closing of program.So here we have seen all the syntax/query to perform various operations on sqlite3 database engine.
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