增删改查

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
import pymysql
txwin_mysql = pymysql.connect(host='xxxx',
user='root',
password='xxx',
database='xxx')
# 获取游标对象
cursor = txwin_mysql.cursor()
sql = 'SELECT * FROM `data` WHERE department=\'xxx\';'
try:
result = cursor.execute(sql)
txwin_mysql.commit()
# print(cursor.fetchall())
'''
etchone():查看一条符合条件的数据,可以连续使用,查询的是上一个fetchone的后面一条
fetchall():查询所有符合条件的数据
fetchmany(3):获取指定的条数数据
'''
for i in cursor.fetchall():
print(i)
print(result) # 输出数量
except Exception as re:
print(re)
txwin_mysql.rollback() # 发生错误时回滚
# 关闭数据库连接
txwin_mysql.close()

MySQL相关基本语句可以参考 MySQL基本语句