Diran
Diran

Reputation: 167

No such column error in 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()

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.

For example self.ID = "hello"

The error would be:

 no such column: "hello" 

Any help would be appreciated, thanks

Upvotes: 1

Views: 776

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169254

Your query looks like:

DELETE FROM Item WHERE ItemID = hello

The error message is helpful in this case.

Instead do:

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

Notes:

  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.

Upvotes: 1

Related Questions