我在 jupyter (python3) 中有以下单元格:
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root',passwd='geheim', db='mysql')
cur = conn.cursor()
cur.execute("DROP DATABASE IF EXISTS foobar")
cur.execute("CREATE DATABASE foobar")
cur.close()
conn.commit()
conn.close()
conn = pymysql.connect(host='localhost', port=3306, user='root',passwd='geheim', db='foobar')
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS map")
cur.execute("CREATE TABLE map (name varchar(64), ID integer, Parent_ID integer)")
map = (("A",1,2),
("B",4,5),
("C",0,0))
print (len(map))
for name,item_id, parent_ID in map:
print (name, item_id, parent_ID)
cur.execute("INSERT INTO map VALUES (\"" + name + "\"," + `item_id` + "," + `parent_ID` + ")")
cur.execute("SELECT * FROM map");
print (cur.fetchall())
cur.close()
conn.commit()
conn.close()
# to toggle linenumbers use CTRL-M L
我相信回想一下代码(INSERT 语句)在 python 2.7 下工作。现在,在转换为 python 3.5 后,我在 INSERT 语句的第一个反引号处收到错误。
我在 python 2.7 中使用了反引号从 to 隐式int
转换string
。
任何线索如何在 python3 中正常工作?