当我使用MySQL工作台时,我可以像这样知道更新数据的结果

当我像这样使用PyMySQL时:
connect = pymysql.Connect(host='localhost', port=3306, user='*', passwd='*', db='MySQL', charset='utf8')
cursor = connect.cursor()
cursor.execute('update table_name set id=0 where id=1')
connect.commit()
connect.close()我怎么知道结果呢?
发布于 2016-02-25 16:37:10
要获得受DML查询(UPDATE、INSERT、DELETE)影响的行数,应该检查cursor.rowcount。要获得匹配的行数,可以在使用相同的WHERE子句执行UPDATE之前运行一个SELECT查询。
import pymysql
conn = pymysql.Connect(user='guest', db='test', autocommit=True)
conn.begin()
cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM test WHERE test_id IN(1, 2, 3)')
print('matched', cursor.fetchone()[0])
cursor.execute('UPDATE test SET value=0 WHERE test_id IN(1, 2, 3)')
print('changed', cursor.rowcount)
conn.rollback()https://stackoverflow.com/questions/35621295
复制相似问题