gpt4 book ai didi

python - 在 python 中执行多次时出错

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:00:35 25 4
gpt4 key购买 nike

我正在学习 python,我是新手。我正在尝试将函数与 mysql 和 python 一起使用,但我可能会遇到错误

这是我的脚本

import MySQLdb
def insert_values(cursor, values):
#cursor = self.connection.cursor()
cursor.executemany("""
insert into pythontest (name1,name2,name3)
values (%s, %s, %s)""", values)
cursor.close()

db = MySQLdb.connect("localhost","root","root","python" )
cursor = db.cursor()

var1 = ['name1','name2','name3']
insert_values(cursor,var1)

db.close()

可能有很多错误,因为我正在学习

1)i don't know how can i pass db object in function or passing cusrsor is ok. because i have to call that function many times in for loop

2)is the syntax of values array ok to go in database

错误

File "mysql.py", line 10, in insert_values
values (%s, %s, %s)""", values)
File "build/bdist.linux-i686/egg/MySQLdb/cursors.py", line 216, in executemany
File "build/bdist.linux-i686/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
_mysql_exceptions.ProgrammingError: not enough arguments for format string

最佳答案

cursor.executemany("""
insert into pythontest (name1,name2,name3)
values (%s, %s, %s)""", *values)

关于python - 在 python 中执行多次时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6080650/

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