我有表示来自不同语言的用户名的数据。我按照以下方式执行了正确的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
\'GG" Cooper"\'
at line 1')
如何正确编码这些类型的字符?
更新
根据提供的答案,我做了以下事项:
\'GG" Cooper"\'
解析用户名
\'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()
答案 0 :(得分:1)
您应该按照以下建议使用参数输入:
http://legacy.python.org/dev/peps/pep-0249/#id15
以下是一个例子:
sql = "insert into foo values(%s)"
cursor.execute(sql, ('My very %$@*@"""S weird name',))