python插入資料庫資料時,報錯%d format: a number is required, not str
阿新 • • 發佈:2020-12-28
python使用pymysql連線資料庫,如果報錯%d format: a number is required, not str,直接把型別改為 %r,表示不管什麼型別的欄位,原樣輸出
例如,我的資料表字段第一個示int型別,插入資料時,我把第一個欄位設定為%d,死活都報錯,最後改為%r,問題解決,真的是浪費時間啊.............................................................................
if __name__ == '__main__':
with MyConnectionPool() as db:
db.cursor.execute("select * from review")
db.conn.commit()
for row in db.cursor.fetchall():
print("nid:%d\t created_at:%s\t content:%s\t source:%s" % row)
insert_sql = "insert into review (nid,created_at,content,source) VALUES (%r,%s,%s,%s)"
insert_data = [(11,"2020.12.28","內容1","中國"),(22,"2020.12.29","2內容","riben")]
# db.cursor.executemany(insert_sql,insert_data)
with MyConnectionPool() as db:
db.cursor.executemany(insert_sql,insert_data)
db.conn.commit()
for row in db.cursor.fetchall():
print("nid:%d\t created_at:%s\t content:%s\t source:%s" % row)