-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
72 lines (57 loc) · 1.65 KB
/
database.py
File metadata and controls
72 lines (57 loc) · 1.65 KB
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
import datetime
import os
import pymysql
host = "localhost"
user = "root"
passwd = "123456"
db = "demo"
table = "t_user"
fields = "name,gender,address,create_time"
createTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(createTime)
try:
conn = pymysql.connect(host=host, user=user, db=db, passwd=passwd)
cursor = conn.cursor()
print('connection success!')
print(' ')
except:
print('connection is failed!')
path = os.getcwd()
print(path)
# save
def insert():
# sql = 'insert into {} ({}) values ({})'.format(table, fields, "'ahian',1,'av','" + createTime+"'")
affectRow = cursor.execute("insert into t_user (name, gender, address, create_time) VALUES (%s,%s,%s,%s)",
('ahian', 1, 'av', createTime))
print(affectRow)
conn.commit()
# query
def select_all():
cursor.execute("select * from t_user")
rows = cursor.fetchall()
print(rows)
# query
def select_by_name(name):
cursor.execute("select * from t_user where name = %s", name)
rows = cursor.fetchone() # only one
# rows = cursor.fetchmany(2) # specified number 1,2,3....
# rows = cursor.fetchall() # all
print(rows)
# update
def update_by_id(name, id):
cursor.execute("update t_user set name = %s where id = %s", (name, id))
conn.commit()
# exception with rollback
def update_with_rollback(name, id):
conn.begin()
cursor.execute("update t_user set name = %s where id = %s", (name, id))
e = 1 / 0
try:
conn.commit()
except ArithmeticError:
conn.rollback()
insert()
select_all()
select_by_name("ahian")
update_by_id("zhangsan", 3)
update_with_rollback("ahian", 3)