-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassitant.py
71 lines (55 loc) · 1.47 KB
/
assitant.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sqlite3
import sys
from sqlite3 import Error
x = sys.argv
string = x[2]
def delete_note(conn, id):
sql = 'DELETE FROM notes WHERE id = ?'
cur = conn.cursor()
cur.execute(sql, (id,))
conn.commit()
def sql_fetch(con):
cursorObj = con.cursor()
cursorObj.execute('SELECT * FROM notes')
rows = cursorObj.fetchall()
for row in rows:
print(row)
def create_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def create_table(conn, create_table_sql):
try:
c = conn.cursor()
c.execute(create_table_sql)
except Error as e:
print(e)
def create_note(conn, note):
sql = ''' INSERT INTO notes(name)
VALUES(?) '''
cur = conn.cursor()
cur.execute(sql, note)
return cur.lastrowid
def main():
note_table = """ CREATE TABLE IF NOT EXISTS notes (
id integer PRIMARY KEY AUTOINCREMENT,
name text NOT NULL
); """
conn = create_connection(r"pythonsqlite.db")
if conn is not None:
create_table(conn, note_table)
else:
print("Error! cannot create the database connection.")
with conn:
if x[2] == "all":
sql_fetch(conn)
elif x[2] == "delete":
delete_note(conn, int(x[3]))
else:
note = (x[2],)
create_note(conn, note)
if __name__ == '__main__':
main()