forked from Humbertzhang/luck2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
34 lines (29 loc) · 1.17 KB
/
db.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
import pymysql
import os
MYSQL_INFO = os.getenv('MYSQL_INFO')
host, port, user, passwd, db = MYSQL_INFO.split('@')
conn = pymysql.connect(host = host,
port = int(port), user = user,
passwd = passwd, db = db,
use_unicode = True, charset = 'utf8')
cur = conn.cursor()
def select_user_viacid(cid, cur):
stu = cur.execute("select * from students where cid=" + str(cid))
if stu is not None:
info = cur.fetchone()
return info
else:
return None
def insert_students(stus, cur):
try:
for stu in stus:
cur.execute("insert into students (cid, name, college, politic, gender, "
"major, birth, national, grade, home) values " + "('" +
str(stu[0]) + "','" + str(stu[1]) + "','" +
str(stu[2]) + "','" + str(stu[3]) + "','" +
str(stu[4]) + "','" + str(stu[5]) + "','" +
str(stu[6]) + "','" + str(stu[7]) + "','" +
str(stu[8]) + "','" + str(stu[9]) + "');")
except pymysql.err.IntegrityError as err:
pass
conn.commit()