2011-01-28 105 views
5

this SO question,我想在python使用以下原料的SQL命令爲「截斷」與某個Django應用程序中的所有表:%混淆Python原始SQL查詢

cursor.execute("set foreign_key_checks = 0") 
cursor.execute("select concat('truncate table ',table_schema,'.',table_name,';') as sql_stmt from information_schema.tables where table_schema = 'my_db' and table_type = 'base table' AND table_name LIKE 'some_prefix%'") 
for sql in [sql[0] for sql in cursor.fetchall()]: 
    cursor.execute(sql) 
cursor.execute("set foreign_key_checks = 1") 

唉我收到以下錯誤:

C:\dev\my_project>my_script.py 
Traceback (most recent call last): 
    File "C:\dev\my_project\my_script.py", line 295, in <module> 
    cursor.execute(r"select concat('truncate table ',table_schema,'.',table_name,';') as sql_stmt from information_schema.tables where table_schema = 'my_db' and table_type = 'base table' AND table_name LIKE 'some_prefix%'") 
    File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 18, in execute 
    sql = self.db.ops.last_executed_query(self.cursor, sql, params) 
    File "C:\Python26\lib\site-packages\django\db\backends\__init__.py", line 216, in last_executed_query 
    return smart_unicode(sql) % u_params 
TypeError: not enough arguments for format string 

是在LIKE製造麻煩的%?我該如何解決它呢?

回答

8

你試過%%嗎?這在Python字符串格式中引用了%。

+2

想要添加的例子/娛樂,請猜這是什麼:'%%%% s %% s%s'%'three'%'two'%'one' – 2011-01-28 23:24:39