Created
October 22, 2018 16:46
-
-
Save HarshaVardhanBabu/6e0a2af745bbf25e2d02b6fd807dd92d to your computer and use it in GitHub Desktop.
sqlite3 operations in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
class DatabaseOperations: | |
def __init__(self): | |
self.database_name = 'phonebook.db' | |
# Get the connection | |
def get_db_connection(self): | |
conn = sqlite3.connect(self.database_name) | |
return conn | |
# Create the database | |
def create_db(self): | |
conn = self.get_db_connection() | |
with conn: | |
cur = conn.cursor() | |
cur.execute("CREATE TABLE if not exists tbl_phonebook ( \ | |
ID INTEGER PRIMARY KEY AUTOINCREMENT,\ | |
col_fname TEXT, \ | |
col_lname TEXT, \ | |
col_fullname TEXT, \ | |
col_phone TEXT, \ | |
col_email TEXT \ | |
);") | |
conn.commit() #must commit() to save changes to the db | |
conn.close() #closing the db connection | |
self.create_temporary_entry() | |
#creating data for our db | |
def create_temporary_entry(self): | |
data = ('John', 'Doe', 'John Doe', '555-555-5555', '[email protected]') #tuple | |
conn = self.get_db_connection() | |
with conn: | |
cur = conn.cursor() | |
cur, count = self.count_records(cur) | |
if count < 1: #if count is 0 then execute the INSERT INTO function into the db | |
cur.execute("""INSERT INTO tbl_phonebook (col_fname, col_lname, col_fullname, col_phone, col_email) VALUES(?,?,?,?,?)""", (data)) | |
conn.commit() | |
conn.close() | |
# count the number of entries in the database | |
def count_records(self,cur): | |
count = "" | |
cur.execute("""SELECT COUNT(*) FROM tbl_phonebook""") | |
count = cur.fetchone()[0] | |
return cur, count | |
# Get the record for the given col_fullname | |
def get_record(self,value): | |
conn = self.get_db_connection() | |
with conn: | |
cursor = conn.cursor() | |
cursor.execute("""SELECT col_fname, col_lname, col_phone, col_email FROM tbl_phonebook WHERE col_fullname = (?)""", [value]) | |
varBody = cursor.fetchall() | |
return varBody[0] | |
# insert a record into the database | |
def insert_record(self,values): | |
conn = self.get_db_connection() | |
var_fname, var_lname, var_phone, var_email = values | |
var_fullname = "{} {}".format(var_fname, var_lname) | |
data = (var_fname, var_lname, var_fullname, var_phone, var_email ) | |
# count = -1 | |
with conn: | |
cursor = conn.cursor() | |
#Checks the db for duplicate fullname | |
#counts the number of fullnames in the db that are the same as the user's current input | |
cursor.execute("""SELECT COUNT(col_fname) FROM tbl_phonebook WHERE col_fullname = '{}'""".format(var_fullname)) | |
count = cursor.fetchone()[0] | |
if(count == 0): | |
cursor.execute("""INSERT INTO tbl_phonebook(col_fname, col_lname, col_fullname, col_phone, col_email) VALUES(?,?,?,?,?)""", (data)) | |
conn.commit() | |
conn.close() | |
return cursor.rowcount | |
def delete_record(self,var_select): | |
conn = self.get_db_connection() | |
with conn: | |
cursor = conn.cursor() | |
cursor.execute("""DELETE FROM tbl_phonebook WHERE col_fullname = '{}'""".format(var_select)) | |
conn.commit() | |
conn.close() | |
return cursor.rowcount | |
if __name__ == "__main__": | |
db = DatabaseOperations() | |
db.create_db() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment