gpt4 book ai didi

Python aiomysql 在我的查询参数周围放置了一组引号

转载 作者:行者123 更新时间:2023-11-29 05:12:50 25 4
gpt4 key购买 nike

我正在使用 python 的 aiomysql 执行函数来执行 sql 查询。

@asyncio.coroutine
def select(sql,args=None,size=None):
log(sql,args)
global __pool
with (yield from __pool) as conn:
cur = yield from conn.cursor()
yield from cur.execute(sql.replace('?','%s'),args or ())
if size:
rs = yield from cur.fetchmany(size)
else:
rs = yield from cur.fetchall()
__pool.close()
yield from __pool.wait_closed()

return rs

在我做的测试函数中

@asyncio.coroutine
def test():
yield from create_pool(loop=loop,user='app',passwd='app')
sql = 'select ? from ?'
arg = ['id','users']
rs = yield from select(sql,arg)
print(rs)

global __pool
__pool.close()
yield from __pool.wait_closed()

在终端我得到一个错误

pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''users'' at line 1")

似乎有一组围绕用户的引用。我试着改变最后一个?到 %s 并且不知何故起作用了,显然是?为 id 工作,但不为用户工作。为什么围绕用户有一套报价,我该如何解决?

谢谢你的回答

最佳答案

SQL 参数不能用于 MySQL 的元数据。您需要清理该值并正常替换它。

关于Python aiomysql 在我的查询参数周围放置了一组引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37050578/

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