-
Notifications
You must be signed in to change notification settings - Fork 0
/
mydb.py
38 lines (31 loc) · 1.4 KB
/
mydb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sqlite3
class Database:
# Initializes/Creates the database and connection
def __init__(self, db):
self.conn = sqlite3.connect(db)
self.cur = self.conn.cursor()
self.cur.execute(
"CREATE TABLE IF NOT EXISTS expense_record (item_name text, item_price float, purchase_date date)")
self.conn.commit()
# Allows us to use any SQL query, and then returns the result
def fetchRecord(self, query):
self.cur.execute(query)
rows = self.cur.fetchall()
return rows
# Inserts a new Record in SQL Table
def insertRecord(self, item_name, item_price, purchase_date):
self.cur.execute("INSERT INTO expense_record VALUES (?, ?, ?)",
(item_name, item_price, purchase_date))
self.conn.commit()
# Removes the Specific Record in SQL Table
def removeRecord(self, rwid):
self.cur.execute("DELETE FROM expense_record WHERE rowid=?", (rwid,))
self.conn.commit()
# Updates the Specific Record in SQL Table
def updateRecord(self, item_name, item_price, purchase_date, rid):
self.cur.execute("UPDATE expense_record SET item_name = ?, item_price = ?, purchase_date = ? WHERE rowid = ?",
(item_name, item_price, purchase_date, rid))
self.conn.commit()
# Closes the Connection upon exit.
def __del__(self):
self.conn.close()