当我使用pymysql对MySQL数据库执行操作时,似乎所有的操作都是临时的,而且只对pymysql连接可见,这意味着我只能通过cur.execute('select * from qiushi')
看到更改,一旦我cur.close()
和{
但是,当我查看增量id号时,它确实增加了,但是我看不到从pymysql连接插入的行。它们好像被自动删除了?!在
我的一些代码在这里:
import pymysql
try:
conn = pymysql.connect(host='127.0.0.1',port=3306,user='pymysql',passwd='pymysql',charset='utf8')
cur = conn.cursor()
#cur.execute('CREATE TABLE qiushi (id INT NOT NULL AUTO_INCREMENT, content_id BIGINT(10) NOT NULL, content VARCHAR(1000), created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(id));')
#cur.execute('DESCRIBE content')
#cur.fetchall()
cur.execute('USE qiushibaike')
for _ in range(0,len(content_ids)):
cur.execute("INSERT INTO qiushi (content,content_id) VALUES (\"%s\",%d)"%(jokes[_],int(content_ids[_])))
finally:
cur.close()
conn.close()
我自己解决了这个问题。。。 因为配置是自动提交的,所以在每个SQL语句之后,我们应该提交更改。在
方法1: 在^{之后添加
cur.commit()
方法2: 编辑连接配置,添加
autocommit=True
相关问题 更多 >
编程相关推荐