python中没有这样的列错误 [英] No such column error in python

查看:51
本文介绍了python中没有这样的列错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    db = sqlite3.connect("SQL database")
    cursor = db.cursor()
    query = 'DELETE FROM Item WHERE ItemID = {}'.format(self.ID)
    cursor.execute(query)
    db.commit()
    cursor.close()

不确定为什么会出现此错误,因为我的代码似乎正确.错误是无论self.ID值是什么,错误都表明没有该值的列.

unsure why this error is coming up as my code seems to be correct. The error is that whatever value self.ID is the error states that that there is no such column that is that value.

例如self.ID ="hello"

For example self.ID = "hello"

错误将是:

 no such column: "hello" 

任何帮助将不胜感激,谢谢

Any help would be appreciated, thanks

推荐答案

您的查询如下:

DELETE FROM Item WHERE ItemID = hello

在这种情况下,错误消息很有用.

The error message is helpful in this case.

代替:

db = sqlite3.connect("SQL database")
cursor = db.cursor()
query = 'DELETE FROM Item WHERE ItemID = ?'
cursor.execute(query, (self.ID,))
db.commit()
cursor.close()

注意:

  1. sqlite3的参数占位符是?.
  2. 参数值应该是 .execute()
  3. 的第二个参数
  4. 该参数应作为序列传递给 .execute().元组就可以了.
  1. The parameter placeholder for sqlite3 is ?.
  2. The parameter value should be the second argument to .execute()
  3. That parameter should be passed to .execute() as a sequence. A tuple is fine.

这篇关于python中没有这样的列错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆