pymysql executemany 也是蛮耿直的

2022-08-23 11:41:28 +08:00
 wuwukai007
book 表  id,name,price
con = pymysq.connect()
db = con.cursor()
data = db.execute("select name,price from origin_book").fetchall()
'''
data= [('x1',100),('x2',200)]
'''
db.execute("insert into book(name,price) values(%s,%s)",data)

2084 次点击
所在节点    Python
4 条回复
chendl111
2022-08-23 12:01:38 +08:00
这是语法问题吧?
kkeep
2022-08-23 13:09:15 +08:00
你 fetchall 出来的('x1',100) 可能并不是 tuple 是 RowProxy 之类的吧 ,只是打印出来是这个样子,
abersheeran
2022-08-23 19:04:11 +08:00
你 fetch 出来的是类 Mapping 的 Row 对象……
xooass
2022-08-24 11:04:19 +08:00
conn = pymysql.connect(
host="127.0.0.1",
user="",password="",
database="",
charset="",
cursorclass=pymysql.cursors.DictCursor
)

cursor = conn.cursor()
cursor.execute("SQL")
data = cursor.fetchall()

ref = data['orders']

不用 tuple ,用 dict 精准命中

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/874752

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX