forked from AmI-2018/python-lab6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_operator.py
143 lines (122 loc) · 4.27 KB
/
db_operator.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import configparser
import pymysql as sql
import sys
config = configparser.ConfigParser()
config.read('config.ini')
confUser = config['mysql']['user']
confPwd = config['mysql']['password']
confDB = config['mysql']['database']
confHost = config['mysql']['host']
def start():
con = sql.connect(user=confUser, password=confPwd, database=confDB, host=confHost)
try:
# print("db correttamente aperto")
# update.message.reply_text("your user id is: " + str(update.message.from_user.id))
cur = con.cursor()
cur.execute(
"create table if not exists 'task' (id integer primary key autoincrement, user_id integer ,todo varchar[255] not null)")
con.commit()
cur.close()
con.close()
except sql.DataError as DataErr:
print("errore di creazione table " + DataErr.args[0])
except sql.DatabaseError as DBerror:
print("errore nell'apertura del db " + DBerror.args[0])
sys.exit(1)
def showTasks(username):
con = sql.connect(user=confUser, password=confPwd, database=confDB, host=confHost)
try:
cur = con.cursor()
sql_query = "select id, todo, urgent from task where username = %s order by todo asc;"
cur.execute(sql_query, (username,))
rows = cur.fetchall()
cur.close()
con.close()
except sql.DataError as DataErr:
print("errore di creazione table " + DataErr.args[0])
except sql.DatabaseError as DBerror:
print("errore nell'apertura del db " + DBerror.args[0])
sys.exit(1)
if len(rows) == 0:
return None
#rows = [i[0] for i in rows]
return rows
def getTaskContent(id):
con = sql.connect(user=confUser, password=confPwd, database=confDB, host=confHost)
try:
cur = con.cursor()
sql_query = "select id, todo, urgent from task where id = %s;"
cur.execute(sql_query, (id,))
row = cur.fetchone()
cur.close()
con.close()
except sql.DataError as DataErr:
print("errore di creazione table " + DataErr.args[0])
except sql.DatabaseError as DBerror:
print("errore nell'apertura del db " + DBerror.args[0])
sys.exit(1)
if row == None:
return ""
# rows = [i[0] for i in rows]
return row
def newTask(todo, username):
# msg = ' '.join(arg)
if (todo != ""):
con = sql.connect(user=confUser, password=confPwd, database=confDB, host=confHost)
cur = con.cursor()
sql_query = "insert into task (todo, username) values (%s, %s)"
cur.execute(sql_query, (todo, username))
con.commit()
cur.close()
con.close()
# showTasks(bot, update)
print("added " + todo + " to the tasks list")
return todo
else:
print("empty task...")
return ""
def updateTask(TaskID, todo, urgent):
if todo != "":
con = sql.connect(user=confUser, password=confPwd, database=confDB, host=confHost)
cur = con.cursor()
sql_query = "UPDATE task SET todo = %s, urgent = %s WHERE id = %s"
ret = cur.execute(sql_query, (todo, urgent, TaskID))
con.commit()
cur.close()
con.close()
return ret
else:
print("empty task...")
return [{'id':'', 'todo':'', 'urgent':''}]
def removeTask(task_id, username):
"""remove a task by givedID"""
# msg = ' '.join(args)
try:
con = sql.connect(user=confUser, password=confPwd, database=confDB, host=confHost)
cur = con.cursor()
sql_query = "delete from task where id = %s AND username=%s;"
res = cur.execute(sql_query, (task_id, username))
con.commit()
cur.close()
con.close()
return res
except ValueError:
print("element not found!")
def removeAllTasks(arg):
con = sql.connect(user=confUser, password=confPwd, database=confDB, host=confHost)
cur = con.cursor()
arg = '%' + arg + '%'
cur.execute("delete from task where todo like %s ", (arg, ))
con.commit()
cur.close()
con.close()
print("Deleted ALL tasks containing " + arg)
if __name__ == '__main__':
print(showTasks("ale"))
#removeAllTasks("task")
#task2 = "task di prova 2"
#newTask(task2)
#print(showTasks())
#ret = showTasks()
#tasks = [i[0] for i in ret]
#print(tasks)