Brian Lee
Brian Lee

Reputation: 57

Multiple placeholder SQLite Python not working

I am trying to UPDATE UnitPrice of a TrackId from Track TABLE. The UnitPrice and the TrackId are given arguments by user input. I am currently receiving an error on number of arguments:

    Traceback (most recent call last):
    File "HW4.py", line 48, in <module>
    conn.execute("UPDATE Track set UnitPrice = ? WHERE TrackId = ?", (n_price,t_id))
    sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

Here is my code:

track_id = raw_input('Enter Track ID\n')
t_id = (track_id,)
cur = conn.execute("SELECT T.UnitPrice FROM Track T WHERE T.TrackId = ?", t_id)
for row in cur:
    print "Unit Price: ", row[0]

new_price = raw_input('Enter New Price\n')
n_price = (new_price,)
conn.execute("UPDATE Track set UnitPrice = ? WHERE TrackId = ?", n_price, t_id)
conn.commit
print "Total number of rows updated: ", conn.total_changes

cur = conn.execute("SELECT T.UnitPrice FROM Track T WHERE T.TrackId = ?", t_id)
for row in cur:
    print "Unice Price: ", row[0]

I'm guessing it is a syntax error on how I am putting n_price and t_id into the ? placeholders.

Upvotes: 1

Views: 1708

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123850

You need to pass in your parameters as a single iterable, like a list or a tuple, but you are wrapping your n_price parameter in another, nested tuple:

n_price = (new_price,)  # this is a tuple
conn.execute("UPDATE Track set UnitPrice = ? WHERE TrackId = ?",
             (n_price, t_id))  # wrapping in another tuple

So now you are passing in ((new_price,), t_id), but that first tuple is not a supported type.

Pass in your arguments together in one tuple:

params = (new_price, t_id)
conn.execute("UPDATE Track set UnitPrice = ? WHERE TrackId = ?", params)

or use

conn.execute("UPDATE Track set UnitPrice = ? WHERE TrackId = ?", (new_price, t_id))

Upvotes: 1

Related Questions