You are on page 1of 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