python执行批量新增sql报错? not enough arguments for format string?
import pyodbc
from sqlConfig import MySQLConnectionPool
# 连接数据库
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\东和.accdb')
# 创建连接池对象
ConnPool = MySQLConnectionPool()
# 创建游标
cursor = conn.cursor()
# 查询数据
cursor.execute('SELECT * FROM 配方列表')
list1 = []
# 逐行读取数据
for row in cursor.fetchall():
list1.append(row)
print(list1)
#list = cursor.fetchall()
sql_update_one = "delete from plan_peifang_list where 1=%s"
ConnPool.update_one(sql_update_one, (1,))
a= [('D033', '吉林建龙', 'AMC72/10Z5-8A2', None, '2024/5/31 9:00:24', None), ('D074', '鞍钢联众', 'MAC65/18F5-10Pa24', None, '2024/4/30 9:12:50', None), ('D033', '吉林建龙', 'AMC72/10Z5-8A2', None, '2024/5/31 9:00:24', None), ('D033', '吉林建龙', 'AM88/6W-L10', None, '2024/5/29 15:31:16', None), ('E005', '塔塔布衫', 'MC14H2Pa24-42C3D3', None, '2024/6/1 15:34:59', None), ('D033', '吉林建龙', 'MC12H4P2-4S9', None, '2024/6/3 6:12:35', None), ('E005', '塔塔布衫', 'MC12H3Pa24-41C3G1', None, '2024/6/3 8:15:18', None), ('E035', '米塔尔', 'MC13H2PA24-4C2', None, '2024/6/3 15:19:34', None), ('E024', '香港泰科', 'MC10H3P2-3D3', None, '2024/6/4 6:53:56', None), ('E039', '瑞钢国际', 'MC14H2PA24-8C2', None, '2024/6/4 14:32:25', None), ('E038', '瑞钢国际', 'MC12H3P2-8C2', None, '2024/6/4 16:00:51', None), ('D033', '吉林建龙', 'AM82/8Z5-L10', None, '2024/6/6 16:33:46', None), ('D023', '北方冶金', 'MC16H2A5', None, '2024/6/6 16:48:42', None), ('D023', '北方冶金', 'MC15F5P2-43H5C6', None, '2024/6/7 14:26:36', None), ('D033', '吉林建龙', 'MAC60/20Y5-10', None, '2024/6/13 17:56:39', 'TRL HMR LSKS'), ('D059', '吉林建龙', 'MAC55/25Y5-8', None, '2024/6/13 10:38:08', 'SWC30H'), ('E038', '瑞钢国际', 'MC12H2P2-8C2', None, '2024/6/8 19:10:57', None), ('E037', '德富高', 'ASC62/10X4-12', None, '2024/6/11 8:09:23', 'DF12X10'), ('D038', '德龙钢铁', 'MC10C1RBA235', None, '2024/6/11 15:17:45', '出钢口'), ('E037', 'DUFERCO', 'ASC73/8B-10A2', None, '2024/6/12 11:07:08', 'DF10K8'), ('E041', '香港泰科', 'MC10H2Pa22-5D3', None, '2024/6/12 20:06:05', 'SWC30H'), ('E000', 'LADLE', 'MC14H3Pa24-4C3', None, '2024/6/13 15:00:17', 'TRL HMR LSKS'), ('E042', '东和欧洲', 'MC12H2PA24', None, '2024/6/13 19:06:19', 'CMAG12H2A4'), ('E042', '东和欧洲', 'MC10C2P2', None, '2024/6/13 22:03:29', 'CMAG 10C2A01')]
print(a)
#sql_insert_all = "insert into plan_peifang_list ('dingdanhao', 'kehu', 'zhilianghao','zhongliang','time','waibuzhilianghao') values (%s,%s,%s,%s,%s,%s)"
sql = "INSERT INTO plan_peifang_list (dingdanhao, kehu, zhilianghao, zhongliang, time, waibuzhilianghao) VALUES (%s, %s, %s, %s, %s, %s)"
ConnPool.insert_all(sql, list1)
我传参用list1 会报错not enough arguments for format string,但是我用变量a就会成功。 a和变量list1的值是一样的啊? 为什么list1会报错?
=========
我改成这个
sql1 = "INSERT INTO plan_peifang_list (dingdanhao, kehu, zhilianghao, zhongliang, time, waibuzhilianghao) VALUES (%s, %s, %s, %s, %s, %s)"
ConnPool.insert(sql1,(row[0], row[1], row[2], row[3], row[4], row[5]))
list1.append(row)
好使了,这是为什么啊?
回复
1个回答
test
2024-08-11
你这个问题的原因简单来说就是因为fetchall得到的row不是tuple类型。
我们在执行批量新增SQL时,出现“not enough arguments for format string”错误通常是由于传递给SQL语句的参数数量不匹配导致的。以下是一个常见的原因和解决方法。
原因:
在使用字符串格式化时,传递的参数数量少于SQL语句中占位符的数量。例如:
sql = "INSERT INTO table_name (column1, column2) VALUES (%s, %s)"
values = [(value1, value2), (value3, )] # 第二个元组少了一个值
解决方法:
确保每个元组中的参数数量与SQL语句中的占位符数量一致。
示例代码:
假设你需要批量插入数据到一个表中,每行数据包含两个字段column1
和column2
。以下是正确的写法:
import psycopg2
# 连接到数据库
conn = psycopg2.connect(
dbname="your_db",
user="your_user",
password="your_password",
host="your_host",
port="your_port"
)
cur = conn.cursor()
# SQL插入语句
sql = "INSERT INTO table_name (column1, column2) VALUES (%s, %s)"
# 批量插入的值
values = [
(value1, value2),
(value3, value4),
(value5, value6)
]
try:
# 执行批量插入
cur.executemany(sql, values)
conn.commit()
except Exception as e:
print(f"An error occurred: {e}")
conn.rollback()
finally:
cur.close()
conn.close()
注意事项:
- 确保每个元组中的参数数量与SQL语句中的占位符数量一致。
- 在批量插入时,使用
executemany
方法而不是循环调用execute
方法,这样效率更高。 - 确保数据库连接和游标在操作完成后关闭。
这样应该可以解决“not enough arguments for format string”错误。如果问题仍然存在,请检查具体的参数传递部分,是不是tuple类型,tuple里面的数量够不够位数,确保所有值都正确地传递到SQL语句中。
回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容