gpt4 book ai didi

python - 我不能得到?而不是 %s 来处理 python mysqldb 的插入

转载 作者:行者123 更新时间:2023-11-28 22:51:40 25 4
gpt4 key购买 nike

我听不懂?而不是 %s 使用 python mysqldb 进行插入。

我试试

cur.execute("""INSERT INTO project (project) VALUES (?) """, ('ham'))

但是我明白了

query = query % db.literal(args)
TypeError: not all arguments converted during string formatting

如果我简单地替换 ?与 %s

我做错了什么?

最佳答案

你不能。 mysqldb 适配器仅支持formatpyformat 参数样式。

Python DB API 2.0 specification支持5 different paramstyle parameter styles但这取决于它们支持的确切数据库适配器实现。 mysqldb project documentation告诉您该适配器支持哪些样式,而 qmark 不是其中之一。

如果您尝试编写适用于多个数据库适配器的 SQL,您很可能需要处理的问题不仅仅是参数样式。考虑使用 SQLAlchemy相反,它可以为您生成与数据库无关的 SQL(在某种程度上)。

关于python - 我不能得到?而不是 %s 来处理 python mysqldb 的插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21281761/

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