在Python中选择MySQL中的数组,不带For循环
我试图从MySQL中选择一个值数组,而不使用for循环。for循环需要很长时间,我想一次获取所有值。我不知道我的论点有什么问题,我很难解释我收到的错误信息是什么意思在Python中选择MySQL中的数组,不带For循环,python,mysql,arrays,Python,Mysql,Arrays,我试图从MySQL中选择一个值数组,而不使用for循环。for循环需要很长时间,我想一次获取所有值。我不知道我的论点有什么问题,我很难解释我收到的错误信息是什么意思 zipcode = ["37204", "60964", "60068"] connection = MySQLdb.connect(host="localhost", user="root", passwd="password", db="database", cursorclass=MySQLdb.cursors.SSCurso
zipcode = ["37204", "60964", "60068"]
connection = MySQLdb.connect(host="localhost", user="root", passwd="password", db="database", cursorclass=MySQLdb.cursors.SSCursor)
cursor = connection.cursor()
query = "SELECT fips FROM us WHERE zip = %s"
cursor.executemany(query,zipcode)
results = cursor.fetchall()
错误如下所示:
query = query % tuple([db.literal(item) for item in args])
TypeError: not all arguments converted during string formatting
非常感谢您的帮助。Python不是我的强项,我以前也没有使用过executemany,但我认为它不应该用于执行应该返回某些内容的代码。您可能希望在查询中使用IN
query = "SELECT fips FROM us WHERE zip IN ('%s')" % "','".join(zipcode)
cursor.execute(query)
results = cursor.fetchall()
在zipcode列表的声明中,逗号位于第一个元素内。这是问题中的输入错误还是代码中的输入错误?我认为这是不安全的,但如果您在其他地方控制了输入,这应该会起作用