Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

Tutorialfor SQliteusing Python

SQLite Python

Uploaded by

Edixson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Tutorialfor SQliteusing Python

SQLite Python

Uploaded by

Edixson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/340938646

Databases with SQlite using Python (Tutorial)

Chapter · April 2020

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.

The user has requested enhancement of the downloaded file.


Databases with SQlite using Python (Tutorial)
Introduction
An organized file for storing data is known as database. A database can be easily accessed and
developed through proper modeling, complex design and techniques. Normally a database can be
manipulated electronically from a computer. To communicate with the database, we need a DataBase
Management System (DBMS), that creates a bridge between the end user and data. DBMS offers a
facility to administer the data inside the database.

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

SQlite can be downloaded according to the operating system here:


https://www.sqlite.org/download.html

The required library is :

https://pypi.org/project/db-sqlite3/

SQlite module

Muhammad Ilyas 1
Connection between python and SQlite

Open command prompt and write: pip install db-sqlite3

Write the following code in python IDE: import sqlite3

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.

[2] Beazley, D. M. (2009). Python essential reference. Addison-Wesley Professional.

Muhammad Ilyas 7

View publication stats

You might also like