Tutorialfor SQliteusing Python
Tutorialfor SQliteusing Python
net/publication/340938646
CITATIONS READS
0 18,692
1 author:
Muhammad Ilyas
Université Paris-Est Créteil Val de Marne - Université Paris 12
15 PUBLICATIONS 54 CITATIONS
SEE PROFILE
All content following this page was uploaded by Muhammad Ilyas on 27 April 2020.
In this section of chapter, we will provide some useful links to install SQlite and the required
documentation for connecting python with existing databases or newly created databases. In these
links, a brief introduction is provided for SQlite. In the second section, steps are described to create
connection between database and python. While in third section, an example for database creation is
provided and in forth section, an exercise is given for practice.
1. Links
A brief introduction for SQlite and related information is briefly explained here:
https://www.sqlite.org/index.html
https://pypi.org/project/db-sqlite3/
SQlite module
Muhammad Ilyas 1
Connection between python and SQlite
Connecting to Database
The following Python code shows how to connect to an existing database. If the database does not exist,
then it will be created and finally a database object will be returned.
We can provide the name of the database here in this section :memory: to create a database in RAM
which can renewed each time. Run the above program to create a database test_abc.db in the directory,
while you can choose any kind of format such csv, text, etc. The path can be modified accordingly. Save
code in test.py file as required and execute the code. After successful execution the following message
will be displayed.
2. Create a Table
The following Python program can create a table in the previously created database. We need to create
the cursor to handle the database.
Muhammad Ilyas 2
INSERT Operation
The Python program below will insert the information to students table created in the example above
SELECT Operation
The following Python program represent how to fetch and display records from students table.
Muhammad Ilyas 3
UPDATE Operation
The following code will update the existing information in the students table.
DELETE Operation
The use of DELETE statement to delete any record from the students table.
Muhammad Ilyas 4
3. Example to practice
Example of creating a database in SQlite using Python
Muhammad Ilyas 5
Function to add data automatically to the database
Muhammad Ilyas 6
4. Exercise
-Create a database for doctors having information about id, first name, last name, joining date, salary,
and address.
-Use a function to manipulate the database such as select statement, insert statement, updating etc.
References:
[1] Krogh, J. W., Krogh, & Gennick. (2018). MySQL Connector/Python Revealed. Apress.
Muhammad Ilyas 7