Python & SQLite3

SQLite is a file based database with full functional capacity like any other Relational Database. Portability is the main advantage of SQLite database. Using sqlite3 library we can manage SQLite database by using Standard Query languages.
Without installing use SQLite database from google Drive by using Colab.
ConnectionCreate database and connection details
Between Records matching a range using between
CaseGrouping Records based on matching conditions
checkAdd, update Constraint CHECK for data validation
insert Add records to table
IIF Condition checking
update Update records of table
delete Delete records from table
distinct Unique data from columns
select SELECT records from table
limit Limiting records returned from table
Order By Limiting records returned from table
Like Matching wildcards to get records from table
check(date)Add, update Constraint CHECK for date validation
SQLite ConnectorTkinter Application to Manage Database and create new database
SQLite BlobManaging binary data by using Blob Column
PygsheetsData from SQLite to Google sheet and vice-versa using Pands DataFrame
ExcelData from SQLite to MS Excel and vice-versa using Pandas DataFrame

SQLAlchemy

deleteDelete records
insertInsert single or multiple records

Basic template using SQLAlchemy with error handling

from sqlalchemy.exc import SQLAlchemyError
q="SELECT * FROM student"
try:
  r_set=my_conn.execute(q)  
  r_list=r_set.fetchall()
except SQLAlchemyError as e:
  error=str(e.__dict__['orig'])
  print(error)
else:
  print("Total Number of rows : ",len(r_list))

Managing Date

For date queries use the fresh copy of SQL dump of date table to apply queries using present date as reference.
Date queryFilter records based on different date values

Creating Database

import sqlite3
my_conn = sqlite3.connect('my_db')
print("Created database successfully");

Drop Database

As SQLite is a file based database, we can delete the file to remove the Database. Here is the code to delete any file from the system.
import os
path='D:\\testing\\my_db\\my_db.db' # update your path
try:
    os.remove(path) # delete file my_db.db 
except OSError as e:
    print(e) # Specific error message 
    print ("Failed to delete %s " % path)
else:
    print ("Successfully deleted the file %s " % path)

Creating table

my_conn.execute('''
CREATE TABLE IF NOT EXISTS student(id integer primary key, 
                      name text, 
                      class text, 
                      mark integer, 
                      gender text 
                      );''')
my_conn.commit()
print("Student Table created successfully");

table structure

r_set=my_conn.execute('''PRAGMA table_info(student);''')
for row in r_set:
    print(row)
Output
(0, 'id', 'integer', 0, None, 1)
(1, 'name', 'text', 0, None, 0)
(2, 'class', 'text', 0, None, 0)
(3, 'mark', 'integer', 0, None, 0)
(4, 'gender', 'text', 0, None, 0)

Getting column names of the table as list

l1=my_conn.execute("PRAGMA table_info(student);")
columns=[r[1] for r in l1] # list of column headers of the table 

Adding records to student table

r_set=my_conn.execute('''INSERT INTO `student` (`id`, `name`, `class`, `mark`, `gender`) VALUES
(1, 'John Deo', 'Four', 75, 'female'),
(2, 'Max Ruin', 'Three', 85, 'male'),
(3, 'Arnold', 'Three', 55, 'male'),
-----------
-----------
(4, 'Krish Star', 'Four', 60, 'female'),
(34, 'Gain Toe', 'Seven', 69, 'male'),
(35, 'Rows Noump', 'Six', 88, 'female');''')
Full code to copy and add all rows to student table is here

Displaying records from student table

r_set=my_conn.execute('''SELECT * from student''');
for row in r_set:
    print(row)
Output
(1, 'John Deo', 'Four', 75, 'female')
(2, 'Max Ruin', 'Three', 85, 'male')
(3, 'Arnold', 'Three', 55, 'male')
---------------
---------------
(34, 'Gain Toe', 'Seven', 69, 'male')
(35, 'Rows Noump', 'Six', 88, 'female')

Total number of records in a table

Used count query without any WHERE condition to get total number of records.
r_set=my_conn.execute("SELECT count(*) as no from STUDENT")
data_row=r_set.fetchone()
nume=data_row[0] # 35

adding a single record

r_set=my_conn.execute('''INSERT INTO `student` 
( `name`, `class`, `mark`, `gender`) VALUES
('New _name', 'Four', 45, 'female')''')

getting id of last inserted row

We can use last_insert_rowid()
x=my_conn.execute('''select last_insert_rowid()''')
id=x.fetchone()
print(id[0])

Deleting records using condition

r_set=my_conn.execute('''DELETE FROM STUDENT WHERE class='Four' ''')
Getting number of rows affected or deleted or updated by recent query. We can use changes()
x=my_conn.execute('''select changes()''')
id=x.fetchone()
print(id[0])

Listing of all tables in the database

r_set=my_conn.execute('''select name from sqlite_master where type = 'table' ''')
for row in r_set:
    print(row)

Listing of all tables with number of records in the database

r_set=my_conn.execute('''select name from sqlite_master 
		where type = 'table' ''')
for row in r_set:
    r_set=my_conn.execute("SELECT count(*) as no from "+ row[0])
    data_row=r_set.fetchone()
    print(row[0],"(",data_row[0],")")
Output
student ( 35 )
category ( 4 )
subcategory ( 16 )
insert
Download .zip file with my_db.db, and .ipynb files
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer