gpt4 book ai didi

python - pymysql 不插入数据;但 "autoincrement"增加

转载 作者:行者123 更新时间:2023-11-28 22:40:41 25 4
gpt4 key购买 nike

这是 https://stackoverflow.com/questions/33336963/use-a-python-dictionary-to-insert-into-mysql/33337128#33337128 的跟进.

import pymysql 
conn = pymysql.connect(server, user , password, "db")
cur = conn.cursor()
ORFs={'E7': '562', 'E6': '83', 'E1': '865', 'E2': '2756 '}
table="genome"
cols = ORFs.keys()
vals = ORFs.values()
sql = "INSERT INTO %s (%s) VALUES(%s)" % (
table, ",".join(cols), ",".join(vals))

print sql
print ORFs.values()
cur.execute(sql)


cur.close()
conn.close()

感谢小痕,我的程序运行正常(即没有报错),但是当我去检查mysql数据库时,数据没有插入。我注意到自动增量 ID 列确实会随着每次失败的尝试而增加。所以这表明我至少在与数据库联系?

一如既往,非常感谢任何帮助

编辑:我包含了 mysql> show create table genome;

的输出
| genome | CREATE TABLE `genome` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`state` char(255) DEFAULT NULL,
`CG` text,
`E1` char(25) DEFAULT NULL,
`E2` char(25) DEFAULT NULL,
`E6` char(25) DEFAULT NULL,
`E7` char(25) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=latin1 |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

最佳答案

我想我明白了。我将在此处添加信息以防其他人遇到此问题:

我需要在脚本中添加conn.commit()

关于python - pymysql 不插入数据;但 "autoincrement"增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33337302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com