Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95527 > unrolled thread
| Started by | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| First post | 2015-08-21 18:39 +0200 |
| Last post | 2015-08-22 11:03 +1000 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.python
Every character of a string becomes a binding Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 18:39 +0200
Re: Every character of a string becomes a binding Zachary Ware <zachary.ware+pylist@gmail.com> - 2015-08-21 11:50 -0500
Re: Every character of a string becomes a binding Cecil Westerhof <Cecil@decebal.nl> - 2015-08-21 19:04 +0200
Re: Every character of a string becomes a binding Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-08-22 11:45 +0200
Re: Every character of a string becomes a binding John Gordon <gordon@panix.com> - 2015-08-21 17:02 +0000
Re: Every character of a string becomes a binding Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-08-21 20:47 -0400
Re: Every character of a string becomes a binding Chris Angelico <rosuav@gmail.com> - 2015-08-22 11:03 +1000
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-08-21 18:39 +0200 |
| Subject | Every character of a string becomes a binding |
| Message-ID | <871tewppdr.fsf@Equus.decebal.nl> |
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?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [next] | [standalone]
| From | Zachary Ware <zachary.ware+pylist@gmail.com> |
|---|---|
| Date | 2015-08-21 11:50 -0500 |
| Message-ID | <mailman.0.1440175848.17298.python-list@python.org> |
| In reply to | #95527 |
On Fri, Aug 21, 2015 at 11:39 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
> 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?
Because the execute method expects the bindings to be passed as a
sequence, which means to pass a single binding you need to wrap it in
a sequence: `c.execute('SELECT url FROM links WHERE url = ?', (url,))`
--
Zach
[toc] | [prev] | [next] | [standalone]
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-08-21 19:04 +0200 |
| Message-ID | <87wpwoo9nr.fsf@Equus.decebal.nl> |
| In reply to | #95528 |
On Friday 21 Aug 2015 18:50 CEST, Zachary Ware wrote:
> On Fri, Aug 21, 2015 at 11:39 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>> 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?
>
> Because the execute method expects the bindings to be passed as a
> sequence, which means to pass a single binding you need to wrap it
> in a sequence: `c.execute('SELECT url FROM links WHERE url = ?',
> (url,))`
Yeah, I found that. I solved it a little differently:
urls = c.execute('SELECT URL FROM links WHERE URL = ?', [url]).fetchall()
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [prev] | [next] | [standalone]
| From | Johannes Bauer <dfnsonfsduifb@gmx.de> |
|---|---|
| Date | 2015-08-22 11:45 +0200 |
| Message-ID | <mr9gci$bh9$3@news.albasani.net> |
| In reply to | #95530 |
On 21.08.2015 19:04, Cecil Westerhof wrote:
>> Because the execute method expects the bindings to be passed as a
>> sequence,
>
> Yeah, I found that. I solved it a little differently:
> urls = c.execute('SELECT URL FROM links WHERE URL = ?', [url]).fetchall()
You continuously ask more than amateurish questions here and frequently,
after having received dozens of helpful and quick replies you respond
with "yeah, I found that" or similar.
Wouldn't it be advisable to tinker with your code roughly 10 more
minutes the next time you run into an amateur's problem instead of
firing off a Usenet post, oh very wise Senior Software Engineer?
Cheers,
Johannes
--
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1@speranza.aioe.org>
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2015-08-21 17:02 +0000 |
| Message-ID | <mr7lit$fpo$1@reader1.panix.com> |
| In reply to | #95527 |
In <871tewppdr.fsf@Equus.decebal.nl> Cecil Westerhof <Cecil@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@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2015-08-21 20:47 -0400 |
| Message-ID | <mailman.6.1440204449.17298.python-list@python.org> |
| In reply to | #95527 |
On Fri, 21 Aug 2015 18:39:28 +0200, Cecil Westerhof <Cecil@decebal.nl>
declaimed the following:
>I have the following with sqlite3:
>urls = c.execute('SELECT URL FROM LINKS WHERE URL = ?', url).fetchall()
>
Well, for one complication... You are asking for the very information
you are providing...
select URL field
where the URL field is equal to some value.
What do you really expect from that? If the table has multiple records
with the same URL value, you will get multiple copies of the same URL.
>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.
So show us the code that creates "url"...
Though I suspect the problem is that "url" is an iterable, and the
DB-API expects the parameters to be provided IN an iterable.
Try using
... where URL=?', (url,) )
to make it a tuple (an iterable) with one item inside it.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-08-22 11:03 +1000 |
| Message-ID | <mailman.8.1440205408.17298.python-list@python.org> |
| In reply to | #95527 |
On Sat, Aug 22, 2015 at 10:47 AM, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:
> On Fri, 21 Aug 2015 18:39:28 +0200, Cecil Westerhof <Cecil@decebal.nl>
> declaimed the following:
>
>>I have the following with sqlite3:
>>urls = c.execute('SELECT URL FROM LINKS WHERE URL = ?', url).fetchall()
>>
> Well, for one complication... You are asking for the very information
> you are providing...
>
> select URL field
> where the URL field is equal to some value.
>
> What do you really expect from that? If the table has multiple records
> with the same URL value, you will get multiple copies of the same URL.
More likely, it's a simple question: "Do you have this URL in your
links?". If you get back a single row, the answer is yes; if you get
back no rows, the answer is no.
ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web