gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:13:49 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/47192173/

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