2011-01-28 16 views
5

Sau this SO question, tôi đang cố gắng để "cắt xén" tất cả các bảng liên quan đến một ứng dụng django nhất định bằng cách sử dụng các lệnh sql liệu sau đây trong python:% confuses sql liệu python truy vấn

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") 

Alas tôi nhận được lỗi sau :

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 

% trong số LIKE có gặp sự cố không? Làm thế nào tôi có thể giải quyết nó?

Trả lời

8

Bạn đã thử %% chưa? Điều đó trích dẫn một% trong định dạng chuỗi Python.

+2

Để biết thêm ví dụ/giải trí, hãy đoán điều này: '%%%% s %% s% s'% 'ba'% 'hai'% 'một' –

+0

Rất đơn giản ... :) – Jonathan