Why does this python class method not work? (it uses SQLite3)

Diran
def update_data(self):

    db = sqlite3.connect("SQLite database")
    cursor = db.cursor()
    cursor.execute("""UPDATE Item SET ? = ? WHERE itemid = ? """,(self.field, self.value, self.ID))
    db.commit()
    cursor.close()

The error states that there is a syntax error near the "?" but I'm not seeing what the issue is.

Note: This is a method of a class with fully defined attributes.

Many Thanks

msw

The ? character is not replaced everywhere it appears, only where an expression can be placed. Thus, trying to use ? for a column name is a sqlite3 syntax error.

You could use

query = 'update Item set {} = ? where itemid = ?'.format(self.field)
cursor.execute(query, (self.value, self.ID))

where, of course, you'd need to scrub self.field for injection attacks.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does ArrayList class uses the name elementData as attribute and as method?

From Dev

why does this java code not work if the method name is different from the class?

From Dev

Why does a simple for loop work in the onCreate method but not in the MainActivity class?

From Dev

Why method uses class name as modifier(?) and argument?

From Dev

Why does this setter method not work?

From Dev

Why does the applicationWillResignActive method not work?

From Dev

Why does this not work? (Python)

From Dev

Python SQLite3: why does indenting the conn.commit() statement have this impact?

From Dev

Why does explicit method specialization of a template class work without its prototype declaration inside the class

From Dev

if Python doesn't support method overloading, then why does this method overload work while this other does not?

From Dev

Why does setattr does not work as a method?

From Dev

Why does the child class does not inherit the method from parent class in python in this example?

From Dev

Python SQLite3 UPDATE with tuple only uses last value

From Dev

How does this javascript class and method work?

From Dev

How does binding parameters work in SQLite3 (with minimal example)?

From Dev

Sqlite3 insert data in database does not work in c

From Dev

Sqlite3 insert data in database does not work in c

From Dev

Sqlite3 insert data in database does not work in c

From Dev

Why does `if Exception` work in Python

From Dev

Why does this indentation work? python

From Dev

Why >= evaluation does not work Python

From Dev

python iterator: why does this work?

From Dev

Why does this python code work?

From Dev

Why does Python.NET use the base method instead of the method from a derived class?

From Dev

Why does this method not work without static?

From Dev

Binding an event to a method, why does it work in UWP?

From Dev

Why does my image preloading method work?

From Dev

Why does extracting this method not work in gradle?

From Dev

Why does my disableButtons method not work?

Related Related

  1. 1

    Why does ArrayList class uses the name elementData as attribute and as method?

  2. 2

    why does this java code not work if the method name is different from the class?

  3. 3

    Why does a simple for loop work in the onCreate method but not in the MainActivity class?

  4. 4

    Why method uses class name as modifier(?) and argument?

  5. 5

    Why does this setter method not work?

  6. 6

    Why does the applicationWillResignActive method not work?

  7. 7

    Why does this not work? (Python)

  8. 8

    Python SQLite3: why does indenting the conn.commit() statement have this impact?

  9. 9

    Why does explicit method specialization of a template class work without its prototype declaration inside the class

  10. 10

    if Python doesn't support method overloading, then why does this method overload work while this other does not?

  11. 11

    Why does setattr does not work as a method?

  12. 12

    Why does the child class does not inherit the method from parent class in python in this example?

  13. 13

    Python SQLite3 UPDATE with tuple only uses last value

  14. 14

    How does this javascript class and method work?

  15. 15

    How does binding parameters work in SQLite3 (with minimal example)?

  16. 16

    Sqlite3 insert data in database does not work in c

  17. 17

    Sqlite3 insert data in database does not work in c

  18. 18

    Sqlite3 insert data in database does not work in c

  19. 19

    Why does `if Exception` work in Python

  20. 20

    Why does this indentation work? python

  21. 21

    Why >= evaluation does not work Python

  22. 22

    python iterator: why does this work?

  23. 23

    Why does this python code work?

  24. 24

    Why does Python.NET use the base method instead of the method from a derived class?

  25. 25

    Why does this method not work without static?

  26. 26

    Binding an event to a method, why does it work in UWP?

  27. 27

    Why does my image preloading method work?

  28. 28

    Why does extracting this method not work in gradle?

  29. 29

    Why does my disableButtons method not work?

HotTag

Archive