By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,359 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,359 IT Pros & Developers. It's quick & easy.

Problem with format string / MySQL cursor

P: n/a
Hello,
I have a string:

INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
`gid`, `password`) VALUES (%s, %s, %s, %s, %i, %i, %s)

that is passed to a MySQL cursor from MySQLdb:

ret = cursor.execute(sql, paras)

paras is:

('flindner', 't****@florian-lindner.de', '/home/flindner/', '/home/
flindner/Mail/test', 1001, 1001, '123')

But that gives me an error:

Traceback (most recent call last):
File "account.py", line 188, in ?
main()
File "account.py", line 33, in main
execute(action, account_type, options)
File "account.py", line 129, in execute
executeSQL(sql, options.username, options.login, options.home,
options.directory, options.uid, options.gid, options.password)
File "/home/flindner/common.py", line 29, in executeSQL
ret = cursor.execute(sql, paras)
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
148, in execute
query = query % db.literal(args)
TypeError: int argument required
I don't see errors in the format string or some other problem....

What's wrong?

Thanks,

Florian

Oct 18 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Oct 19, 7:32 am, Florian Lindner <Florian.Lind...@xgm.dewrote:
Hello,
I have a string:

INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
`gid`, `password`) VALUES (%s, %s, %s, %s, %i, %i, %s)

that is passed to a MySQL cursor from MySQLdb:

ret = cursor.execute(sql, paras)

paras is:

('flindner', 'te...@florian-lindner.de', '/home/flindner/', '/home/
flindner/Mail/test', 1001, 1001, '123')

But that gives me an error:

Traceback (most recent call last):
File "account.py", line 188, in ?
main()
File "account.py", line 33, in main
execute(action, account_type, options)
File "account.py", line 129, in execute
executeSQL(sql, options.username, options.login, options.home,
options.directory, options.uid, options.gid, options.password)
File "/home/flindner/common.py", line 29, in executeSQL
ret = cursor.execute(sql, paras)
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
148, in execute
query = query % db.literal(args)
TypeError: int argument required

I don't see errors in the format string or some other problem....

What's wrong?

Thanks,

Florian
You should be using '?' for parameter bindings in your sql string not
python format specifiers (you are after all writing sql here not
python).

INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
`gid`, `password`) VALUES (?, ?, ?, ?, ?, ?, ?)

Cheers
Tim
Oct 18 '07 #2

P: n/a
ti******@gmail.com wrote:
On Oct 19, 7:32 am, Florian Lindner <Florian.Lind...@xgm.dewrote:
>Hello,
I have a string:

INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
`gid`, `password`) VALUES (%s, %s, %s, %s, %i, %i, %s)

that is passed to a MySQL cursor from MySQLdb:

ret = cursor.execute(sql, paras)

paras is:

('flindner', 'te...@florian-lindner.de', '/home/flindner/', '/home/
flindner/Mail/test', 1001, 1001, '123')

But that gives me an error:

Traceback (most recent call last):
File "account.py", line 188, in ?
main()
File "account.py", line 33, in main
execute(action, account_type, options)
File "account.py", line 129, in execute
executeSQL(sql, options.username, options.login, options.home,
options.directory, options.uid, options.gid, options.password)
File "/home/flindner/common.py", line 29, in executeSQL
ret = cursor.execute(sql, paras)
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
148, in execute
query = query % db.literal(args)
TypeError: int argument required

I don't see errors in the format string or some other problem....

What's wrong?
You should be using '?' for parameter bindings in your sql string not
python format specifiers (you are after all writing sql here not
python).

INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
`gid`, `password`) VALUES (?, ?, ?, ?, ?, ?, ?)


Sorry Tim, but that isn't correct:
>>import MySQLdb
MySQLdb.paramstyle

'format'

Florian, what happens when you replace your %i with %s?
--
pkm ~ http://paulmcnett.com

Oct 18 '07 #3

P: n/a
On 18 Okt., 22:08, Paul McNett <p...@ulmcnett.comwrote:
timar...@gmail.com wrote:
On Oct 19, 7:32 am, Florian Lindner <Florian.Lind...@xgm.dewrote:
Hello,
I have a string:
INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
`gid`, `password`) VALUES (%s, %s, %s, %s, %i, %i, %s)
that is passed to a MySQL cursor from MySQLdb:
ret = cursor.execute(sql, paras)
paras is:
('flindner', 'te...@florian-lindner.de', '/home/flindner/', '/home/
flindner/Mail/test', 1001, 1001, '123')
But that gives me an error:
Traceback (most recent call last):
File "account.py", line 188, in ?
main()
File "account.py", line 33, in main
execute(action, account_type, options)
File "account.py", line 129, in execute
executeSQL(sql, options.username, options.login, options.home,
options.directory, options.uid, options.gid, options.password)
File "/home/flindner/common.py", line 29, in executeSQL
ret = cursor.execute(sql, paras)
File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line
148, in execute
query = query % db.literal(args)
TypeError: int argument required
I don't see errors in the format string or some other problem....
What's wrong?
You should be using '?' for parameter bindings in your sql string not
python format specifiers (you are after all writing sql here not
python).
INSERT INTO mailboxes (`name`, `login`, `home`, `maildir`, `uid`,
`gid`, `password`) VALUES (?, ?, ?, ?, ?, ?, ?)

Sorry Tim, but that isn't correct:
>import MySQLdb
MySQLdb.paramstyle

'format'

Florian, what happens when you replace your %i with %s?
That works! Thanks! But a weird error message for this solution...

Oct 18 '07 #4

P: n/a
En Fri, 19 Oct 2007 05:32:04 -0300, Dennis Lee Bieber
<wl*****@ix.netcom.comescribió:
On Thu, 18 Oct 2007 13:40:53 -0700, Florian Lindner
<Fl*************@xgm.dedeclaimed the following in comp.lang.python:
>That works! Thanks! But a weird error message for this solution...

Not really... The MySQLdb adapter converts all parameters to
properly delimited STRINGS (I don't know of any SQL system that passes
/binary/ numerics). But MySQLdb also uses Python % formatting to then
I'd say the opposite: only poorly implemented systems convert numeric
arguments to text.
*Real* bound variables are passed as pointers. The SQL sentence can be
prepared and re-used with different sets of values several times, without
having to re-parse it again and again.
If the MySQLdb adapter actually converts and inserts the arguments itself
into the supplied SQL sentence, generating a new string for each call,
this advantage -and many others- are cancelled, defeating the purpose of
bound variables.

--
Gabriel Genellina

Oct 19 '07 #5

P: n/a
Dennis Lee Bieber wrote:
I should also add that I'm still using a somewhat older MySQLdb (one
reason I haven't upgraded Python to 2.5 series -- finding trusted binary
builds of the third-party stuff is a pain...)
What, you don't trust the build from a World of Warcraft guild?

John Nagle
Oct 22 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.