【问题标题】:SQLite3 syntax error debugging [duplicate]SQLite3语法错误调试[重复]
【发布时间】:2015-11-30 22:01:26
【问题描述】:

我很好奇,如何调试 SQLite3 语法。我每天都在使用这个 SQL,有时我会遇到无法修复的语法错误。

例如这个查询:

    self.cur.execute("""INSERT INTO table(?,?,?,?,?,?,?,?,?,?,?,?,?) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)""", ('category', 'product_number', 'product', 'availability', 'manufacturer', 'weight', 'inner', 'outer', 'width', 'list_price', 'discount', 'gross_price', 'net_price', u'Wellenscheiben BKAGI..', 'BKAGI15', '-', 'This', 'PePS', '138,00', '50,00', '78,00', '6,50', '36,93', '55,00', '19,78', '16,62'))

sqlite3.OperationalError:靠近“?”:语法错误

你知道如何找出错误在哪里吗?

【问题讨论】:

  • 您不能(也不应该)将查询参数用于标识符(表名和列名)。

标签: python sqlite


【解决方案1】:

您是否尝试设置此标志:

sqlite3.enable_callback_tracebacks(True)

这里是文档:https://docs.python.org/2/library/sqlite3.html#sqlite3.enable_callback_tracebacks

编辑:尝试过自己,在这种情况下并没有太大帮助。您也可以尝试记录执行的 sql,如下所述:https://stackoverflow.com/a/13647368/1338845,然后查看该 sql 是否应该是。

【讨论】:

    【解决方案2】:

    我不清楚您是否只需要一般调试建议或专门针对该问题的帮助。如果是后者:

    您似乎只能将? 占位符用于值,而不能用于列名。直接在字符串中包含列,或者如果您试图缩短语句的长度以提高可读性,例如:

    cols = ','.join('category', 'product_number', 'product', 'availability', 'manufacturer', 'weight', 'inner', 'outer', 'width', 'list_price', 'discount', 'gross_price', 'net_price')
    self.cur.execute('INSERT INTO table({}) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)'.format(cols), (u'Wellenscheiben BKAGI..', 'BKAGI15', '-', 'This', 'PePS', '138,00', '50,00', '78,00', '6,50', '36,93', '55,00', '19,78', '16,62'))
    

    【讨论】:

      【解决方案3】:

      规则一般是打开一个终端(Windows下的控制台),在sqlite3(或sqlite3.exe)中输入命令。

      或者,当问题出现在像这里这样的变量替换时,它当然没什么用。但是 SQL 只允许对 进行这种变量替换,而不是表或列名。

      因此,对于您的示例,正​​确的语法是:

      self.cur.execute("""INSERT INTO table(category, product_number, product,
              availability, manufacturer, weight, inner, outer, width, list_price,
              discount, gross_price, net_price) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)""",
          u"Wellenscheiben BKAGI..", "BKAGI15', '-', 'This', 'PePS', '138,00', '50,00', 
          '78,00', '6,50', '36,93', '55,00', '19,78', '16,62'))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多