forked from yinxiaojian/emotion-recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
41 lines (32 loc) · 1.06 KB
/
database.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
import pymysql
# connect database
def connect():
db = pymysql.connect("localhost", "root", "yjw715901", "PROJECT")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL,如果表存在则删除
cursor.execute("DROP TABLE IF EXISTS EMOTION")
# 使用预处理语句创建表
sql = """CREATE TABLE EMOTION (
`id` INT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT ,`LIST` INT NOT NULL , PRIMARY KEY ( `id` )
)"""
cursor.execute(sql)
# 关闭数据库连接
db.close()
def insert(id, data):
db = pymysql.connect("localhost", "root", "yjw715901", "PROJECT")
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 插入语句
sql = "INSERT INTO EMOTION(id, LIST) VALUES('%s','%s')" % (id, data)
try:
# 执行sql语句
cursor.execute(sql)
# 执行sql语句
db.commit()
except:
# 发生错误时回滚
print('insert error')
db.rollback()
finally:
db.close()