gpt4 book ai didi

Python unicode 转义下划线和双引号

转载 作者:行者123 更新时间:2023-11-29 12:44:24 26 4
gpt4 key购买 nike

我有代表不同语言的用户名的数据。我已经执行了正确的 unicoding 过程,如下所示:

while attempts < 3 and not success:
query = ur'''select gu_name from globaluser where gu_name = "{uname}"'''.format(uname=unicode(filerow['user_name'],'utf-8', errors='strict'))
try:
self.gdbCursor.execute(query.encode('utf-8'))
gUser = self.gdbCursor.fetchone()

但是当涉及到像这样的名字时Name1_"GG"_Name1AnotherName我最终收到以下错误:

ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near <code>\'GG" Cooper"\'</code> at line 1')

如何正确编码这些类型的字符?

更新:

根据提供的答案,我执行了以下操作:

\'GG" Cooper"\'解析用户名

                    while attempts < 3 and not success:
#query = ur'''select gu_name from globaluser where gu_name = "{uname}"'''.format(uname=unicode(filerow['user_name'],'utf-8', errors='strict'))
uName = unicode(filerow['user_name'], 'utf-8')
query = ur'''select gu_name from globaluser where gu_name = "%s"'''
try:
#self.gdbCursor.execute(query.encode('utf-8'))
self.gdbCursor.execute((query % (uName)).encode('utf-8'))
gUser = self.gdbCursor.fetchone()

但我仍然收到以下错误:

ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near \'GG" Cooper"\' at line 1')

最佳答案

您应该按照以下建议使用参数输入:

http://legacy.python.org/dev/peps/pep-0249/#id15

这是一个例子:

sql = "insert into foo values(%s)"
cursor.execute(sql, ('My very %$@*@"""S weird name',))

关于Python unicode 转义下划线和双引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25651785/

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