tags:

views:

2953

answers:

6

Using SQLite3 with Python 2.5, I'm trying to iterate through a list and pull the weight of an item from the database based on the item's name.

I tried using the "?" parameter substitution suggested to prevent SQL injections but it doesn't work. For example, when I use:

for item in self.inventory_names:
    self.cursor.execute("SELECT weight FROM Equipment WHERE name = ?", item)
    self.cursor.close()

I get the error:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 8 supplied.

I believe this is somehow caused by the initial creation of the database; the module I made that actually creates the DB does have 8 bindings.

cursor.execute("""CREATE TABLE Equipment 
    (id INTEGER PRIMARY KEY, 
    name TEXT,
    price INTEGER, 
    weight REAL, 
    info TEXT, 
    ammo_cap INTEGER, 
    availability_west TEXT,
    availability_east TEXT)""")

However, when I use the less-secure "%s" substitution for each item name, it works just fine. Like so:

for item in self.inventory_names:
    self.cursor.execute("SELECT weight FROM Equipment WHERE name = '%s'" % item)
    self.cursor.close()

I can't figure out why it thinks I have 8 bindins when I'm only calling one. How can I fix it?

+1  A: 

have You tried this ? :

for item in self.inventory_names:
    t = (item,)
    self.cursor.execute("SELECT weight FROM Equipment WHERE name = ?", t)
    self.cursor.close()

cursor.execute() expects a sequence (list,tuple) as second parameter. (-> ddaa )

Blauohr
+17  A: 

The Cursor.execute() method expects a sequence as second parameter. You are supplying a string which happens to be 8 characters long.

Use the following form instead:

self.cursor.execute("SELECT weight FROM Equipment WHERE name = ?", [item])

Python library reference 13.13.3: sqlite3 Cursor Objects.

ddaa
+1  A: 

I have spent half a day trying to figure out why something like this would give me an error:

cursor.execute("SELECT * from ? WHERE name = ?", (table_name, name))

only to find out that table names cannot be parametrized. Hope this will help other people save some time.

A: 

How do you do "like" statements like this?

i.e. I'm having trouble with something akin to

execute("select * from table where column like '%%?%%', (val,))

This appears to be due to the ? being quoted (but it has to be in SQL syntax... I try without I get errors, without the % chars and it's operating like 'where x=y')

Dan
A: 

Quoting (is that what the parens mean?) the ? with parens seems to work for me. I kept trying with (literally) '?' but I kept getting

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 1 supplied.

When I did:

SELECT fact FROM factoids WHERE key LIKE (?)

instead of:

SELECT fact FROM factoids WHERE key LIKE '?'

It worked.

Is this some python 2.6 thing?

kjikaqawej
A: 

Try execute("select fact from factoids where key like ?", "%%s%" % val)

You don't wrap anything around the ? at all... python sqlite will correctly convert it into a quoted entity.

JBE