5

このSOの質問に続いて、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

問題が発生%していますか?LIKEどうすれば回避できますか?

4

1 に答える 1

8

やってみました %%?これは、Python 文字列フォーマットで % を引用します。

于 2011-01-28T23:21:54.580 に答える