471,338 Members | 1,019 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Problem with format string / MySQL cursor

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
5 5344
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
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
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
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
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.

Similar topics

1 post views Thread by Fons Dijkstra | last post: by
5 posts views Thread by Lad | last post: by
reply views Thread by Daniel Crespo | last post: by
3 posts views Thread by cnplnsk | last post: by
1 post views Thread by erikcw | last post: by
3 posts views Thread by len | last post: by
reply views Thread by Edwin.Madari | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.