Every character of a string becomes a binding

John Gordon gordon at panix.com
Fri Aug 21 19:02:21 CEST 2015


In <871tewppdr.fsf at Equus.decebal.nl> Cecil Westerhof <Cecil at decebal.nl> writes:

> I have the following with sqlite3:
> urls = c.execute('SELECT URL FROM LINKS WHERE URL = ?', url).fetchall()

> But this gives:
> Traceback (most recent call last):
>   File "./createDB.py", line 52, in <module>
>     urls = c.execute('SELECT URL FROM LINKS WHERE URL = ?', url).fetchall()
> sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 40 supplied.

> The number of bindings is the length of the string.
> What is happening here? Why is every character of the string seen as a
> binding, instead of the string being the binding?

The second argument to execute() is supposed to be a sequence (usually a
tuple) containing the items to be used a parameters in the SQL statement.

You're probably defining url like this:

    url = 'http://somewhere.com/something'

But it should be:

    url = ('http://somewhere.com/something', )

In which case, I wouldn't call it "url", but rather "args" or something
like that.

To answer your stated question: a string *is* a sequence, so technically
you're passing the correct sort of object as the second argument to
execute().  But the SQL statement only asks for one parameter, so sqlite
is wondering where it should put the other 39 items.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"



More information about the Python-list mailing list