%混淆python原始sql查询

%混淆python原始sql查询,python,rawsql,sql-like,Python,Rawsql,Sql Like,下面,我尝试使用python中的以下原始sql命令“截断”与某个django应用程序相关的所有表: 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' an

下面,我尝试使用python中的以下原始sql命令“截断”与某个django应用程序相关的所有表:

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
C:\dev\my\u project>my\u script.py
回溯(最近一次呼叫最后一次):
文件“C:\dev\my\u project\my\u script.py”,第295行,在
执行(r“选择concat('truncate table',table_schema',.”,table_name',;'))作为sql_stmt,从信息_schema.tables中选择,其中table_schema='my_db'和table_type='base table'和table_name,如'some_prefix%'))
文件“C:\Python26\lib\site packages\django\db\backends\util.py”,执行中的第18行
sql=self.db.ops.last_executed_查询(self.cursor,sql,params)
文件“C:\Python26\lib\site packages\django\db\backends\\ uuuuu init\uuuuuu.py”,第216行,在上次执行的查询中
返回智能\u unicode(sql)%u参数
TypeError:格式字符串的参数不足

中的
%
像是在制造麻烦吗?我如何解决这个问题?

你试过%%吗?它在Python字符串格式中引用了一个%。

要增加示例/娱乐,请猜猜它的作用:'%%%s%%s%s%%三个''%2''%1'