0
我使用https://github.com/PyMySQL/PyMySQL來連接MySQL。我想在MySQL命令中執行「like」查詢。如何在我的MySQL查詢中執行'%'
下面是命令:
"select * from table where c1 > '2015-11-01' and c2 not like '%word%'"
我有一個具有列C1,C2和C3表table
。
import pymysql.cursors
def query_my(my_query):
connection = pymysql.connect(host='',
user='',
passwd='',
db='',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
sql = "select c3 from table where c1 > %s and c2 not like '%word%'"
cursor.execute(sql, (my_query))
re = cursor.fetchall()
return re
connection.commit()
finally:
connection.close()
my_query='2015-11-01'
query_my(my_query)
,它會返回此錯誤:
Traceback (most recent call last):
File "/home/y/share/htdocs/cgi/weekly_report_v2.py", line 31, in <modul
print query_inc_group(i_start,i_stop)
File "/home/y/share/htdocs/cgi/weekly_report_v2.py", line 18, in query_inc_group
cursor.execute(sql, (_i_start,_i_stop))
File "/usr/lib/python2.6/site-packages/PyMySQL-0.6.6-py2.6.egg/pymysql/cursors.py", line 143, in execute
query = self.mogrify(query, args)
File "/usr/lib/python2.6/site-packages/PyMySQL-0.6.6-py2.6.egg/pymysql/cursors.py", line 134, in mogrify
query = query % self._escape_args(args, conn)
TypeError: not enough arguments for format string
我認爲這個問題是從not like '%word%'
。我該如何解決這種錯誤?我直接打印了SQL,結果看起來不錯。
文件 「/usr/lib/python2.6/site-packages/PyMySQL-0.6.6-py2.6.egg/pymysql/cursors.py」,線路134,在mogrify 查詢=查詢% self._escape_args(args,conn) 此行導致錯誤,因爲它使用python String格式替換sql中的參數。 – shazin
它的工作原理!但爲什麼?一種逃避的方式?我打印了'sql',它顯示%% double% –
非常感謝您的回答! –