0% found this document useful (0 votes)
47 views7 pages

Database Prgs

The document contains Python code for connecting to a SQLite database and defining functions to perform CRUD (create, read, update, delete) operations. It includes functions to fetch all rows, limited rows, and single rows from a table. It also includes functions to insert a single record, insert records with variables, insert multiple records, and update a record in the table. The code is organized into modules for select, insert, and main queries with an __init__.py to import the modules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views7 pages

Database Prgs

The document contains Python code for connecting to a SQLite database and defining functions to perform CRUD (create, read, update, delete) operations. It includes functions to fetch all rows, limited rows, and single rows from a table. It also includes functions to insert a single record, insert records with variables, insert multiple records, and update a record in the table. The code is organized into modules for select, insert, and main queries with an __init__.py to import the modules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Code for the __init__.

py
from .selectquery import *
from .insertquery import *
Code for the selectquery.py
import sqlite3

#UDF to fetch all rows


def getAllRows():
try:
connection = sqlite3.connect('c:\sqlite\school.db')
cursor = connection.cursor()
print("Connected to SQLite")

sqlite_select_query = "SELECT * from students"


cursor.execute(sqlite_select_query)
records = cursor.fetchall()
print("Total rows are: ", len(records))
print("Printing each row")

for row in records:


print("StudentId: ", row[0])
print("StudentName: ", row[1])
print("DepartmentId: ", row[2])
print("DateOfBirth: ", row[3])
print("\n")

cursor.close()

except sqlite3.Error as error:


print("Failed to read data from table", error)
finally:
if connection:
connection.close()
print("The Sqlite connection is closed")
#UDF to fetch specific rows
def getlimitedRows(size):
try:
connection = sqlite3.connect('c:\sqlite\school.db')
cursor = connection.cursor()
print("Connected to database")

sqlite_select_query = """SELECT * from students"""


cursor.execute(sqlite_select_query)
records = cursor.fetchmany(size)
print("Fetching Total ", size," rows")
print("Printing each row")
for row in records:
print("StudentId: ", row[0])
print("StudentName: ", row[1])
print("DepartmentId: ", row[2])
print("DateOfBirth: ", row[3])
print("\n")

cursor.close()

except sqlite3.Error as error:


print("Failed to read data from table", error)
finally:
if connection:
connection.close()
print("The Sqlite connection is closed")

#UDF to fetch single rows

def getSingleRows():
try:
connection = sqlite3.connect('c:\sqlite\school.db')
cursor = connection.cursor()
print("Connected to database")

sqlite_select_query = """SELECT * from students"""


cursor.execute(sqlite_select_query)
print("Fetching single row")
record = cursor.fetchone()
print(record)

print("Fetching next row")


record = cursor.fetchone()
print(record)

cursor.close()

except sqlite3.Error as error:


print("Failed to read data from table", error)
finally:
if connection:
connection.close()
print("The Sqlite connection is closed")

code for insertquery.py


import sqlite3
#UDF to insert a rows

def insertrow():
try:
connection = sqlite3.connect('c:\sqlite\school.db')
cursor = connection.cursor()
print("Successfully Connected to SQLite")

sqlite_insert_query = """INSERT INTO students


(StudentId, StudentName,DepartmentId,DateOfBirth)
VALUES
(109,'Shivansh',2,'2019-03-17')"""

count = cursor.execute(sqlite_insert_query)
connection.commit()
print("Record inserted successfully into Student table ", cursor.rowcount)
cursor.close()

except sqlite3.Error as error:


print("Failed to insert data into sqlite table", error)
finally:
if connection:
connection.close()
print("The SQLite connection is closed")

def insertVaribleIntoTable(id, name, email, joinDate, salary):


try:
sqliteConnection = sqlite3.connect('c:\sqlite\school.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")

sqlite_insert_with_param = """INSERT INTO Students


(StudentId, StudentName,DepartmentId,DateOfBirth)
VALUES (?, ?, ?, ?);"""

data_tuple = (StudentId, StudentName,DepartmentId,DateOfBirth)


cursor.execute(sqlite_insert_with_param, data_tuple)
sqliteConnection.commit()
print("Python Variables inserted successfully into Students table")

cursor.close()

except sqlite3.Error as error:


print("Failed to insert Python variable into sqlite table", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")

#UDF to insert multiple rows

def insertMultipleRecords(recordList):
try:
sqliteConnection = sqlite3.connect('c:\sqlite\school.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")

sqlite_insert_query = """INSERT INTO Students


(StudentId, StudentName,DepartmentId,DateOfBirth)
VALUES (?, ?, ?, ?);"""

cursor.executemany(sqlite_insert_query, recordList)
sqliteConnection.commit()
print("Total", cursor.rowcount, "Records inserted successfully into
SqliteDb_developers table")
sqliteConnection.commit()
cursor.close()

except sqlite3.Error as error:


print("Failed to insert multiple records into sqlite table", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")

recordsToInsert = [(4, 'Jyoti',2, '2019-01-14', 9500),


(5, 'Moksh', 3, '2019-05-15', 7600),
(6, 'Preeti', 2, '2019-03-27', 8400)]
#UDF to update a rows

def updateSqliteTable():
try:
sqliteConnection = sqlite3.connect('c:\sqlite\school.db')
cursor = sqliteConnection.cursor()
print("Connected to SQLite")

sql_update_query = """Update Students set DepartmentId = 10000 where StudentId


= 4"""
cursor.execute(sql_update_query)
sqliteConnection.commit()
print("Record Updated successfully ")
cursor.close()

except sqlite3.Error as error:


print("Failed to update sqlite table", error)
finally:
if sqliteConnection:
sqliteConnection.close()
print("The SQLite connection is closed")
code for using the modules
import pythonDB as a

a.getlimitedRows(2)
a.getSingleRows()
a.insertrow()
a.getAllRows()

You might also like