-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmydb.py
33 lines (27 loc) · 1.15 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
#1 module
import sqlite3
#2 class, constructor and methods
class Database:
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()
def fetchRecord(self, query):
self.cur.execute(query)
rows = self.cur.fetchall()
return rows
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()
def removeRecord(self, rwid):
self.cur.execute("DELETE FROM expense_record WHERE rowid=?", (rwid,))
self.conn.commit()
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()
def __del__(self):
self.conn.close()