SQLite is a file-based database that offers the full functionality of a traditional relational database. Its standout feature is portability, making it exceptionally convenient for a wide range of applications.
The sqlite3 library enables the management of SQLite databases through standard SQL queries, further enhancing its usability and flexibility. This combination of portability and full SQL capabilities makes SQLite a popular choice for developers looking for a lightweight, yet powerful, database solution.
Without installing use SQLite database from google Drive by using Colab.
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.
Using Tkinter :
Tkinter provides a GUI environment for users. We can use Tkinter window to manage SQLite database by developing different applications. Check here for Tkinter and SQLite applications.
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)
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],")")