def __init__(self): self.conn = db.connectdb('postgres') if not (db.execute_sql(self.conn, sql.sql_db_exists, fetch=True)[1][0][0]): print("Contact book db does not exists, creating a new one now...") self.conn.autocommit = True db.execute_sql(self.conn, sql.sql_create_db) self.conn = db.connectdb() db.execute_sql(self.conn, sql.sql_create_table) else: print("contactbook db exists, connecting...") self.conn = db.connectdb() self.conn.autocommit = True
def displaycontacts(self): data = db.execute_sql(self.conn, sql.sql_select_all, fetch=True) print( "......................................................................................." ) print("%5s%50s\t%5s\t%10s" % ("Contact ID", "Contact Name", "Ext", "Contact Number")) for row in data[1]: print('\n') print("%5s %50s" % (row[0], row[1]), end="\t") ext = row[2] num = row[3] for i, e in enumerate(ext): if i == 0: print("%5s\t%10s" % (e, num[i])) else: print("%5s %50s\t%5s\t%10s" % (" ", " ", e, num[i])) print( "......................................................................................." )
def deletedb(self): self.conn = db.connectdb('postgres') self.conn.autocommit = True db.execute_sql(self.conn, sql.sql_delete_db) print("contactbook db deleted.") exit()
def resetdb(self): db.execute_sql(self.conn, sql.sql_remove_table) db.execute_sql(self.conn, sql.sql_create_table) print("Contact Book reset.")
def deleteall(self): db.execute_sql(self.conn, sql.sql_delete_all) print("All contact records deleted.")
def deletecontact(self, name): db.execute_sql(self.conn, sql.sql_delete_contact(name.upper())) self.conn.commit() print(name, " record deleted.")
def insertrow(self, name, exts, nums): db.execute_sql(self.conn, sql.sql_insert_contact(name, exts, nums))