【问题标题】:% confuses python raw sql query% 混淆 python 原始 sql 查询
【发布时间】:2014-07-02 09:58:51
【问题描述】:

this SO question 之后,我正在尝试在 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 中的% 有问题吗?我该如何解决?

【问题讨论】:

    标签: python rawsql sql-like


    【解决方案1】:

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

    【讨论】:

    • 对于添加的示例/娱乐,猜猜这是做什么的:'%%%%s %%s %s' % 'three' % 'two' % 'one'
    猜你喜欢
    • 1970-01-01
    • 2015-04-16
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    相关资源
    最近更新 更多