-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
49 lines (42 loc) · 1.51 KB
/
model.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
39
40
41
42
43
44
45
46
47
48
import sqlite3
def connectToDB():
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS todo
(todo_id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
status INTEGER NOT NULL,
title2 TEXT NOT NULL)''')
# reset the auto-increment counter after clearing all records
cursor.execute("DELETE FROM sqlite_sequence WHERE name='todo'")
conn.commit()
conn.close()
def insertDB(title,title2):
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute("INSERT INTO todo (title,status,title2) VALUES (?, ?,?)", (title, 0,title2))
conn.commit()
conn.close()
def viewData():
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM todo")
rows = cursor.fetchall()
return rows
def updateData(id):
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
# cursor.execute('DELETE FROM todo')
cursor.execute("UPDATE todo SET status = 1 WHERE todo_id=?", (id,))
conn.commit()
conn.close()
def deleteData(id):
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
cursor.execute("DELETE FROM todo WHERE todo_id=?", (id,))
cursor.execute("SELECT todo_id FROM todo")
rows = cursor.fetchall()
for i, row in enumerate(rows, start=1):
cursor.execute("UPDATE todo SET todo_id=? WHERE todo_id=?", (i, row[0]))
conn.commit()
conn.close()