? and %s placeholders, help?
Duncan Smith
buzzard at urubu.freeserve.co.uk
Sun Jun 23 20:19:11 EDT 2002
Can anyone please explain the following. Basically I need to know the
difference between the ? placeholder and the %s placeholder. I have only
been able to find examples of the use of '?' in situations where it clearly
works (integers?). But I have no good idea why I cannot use it as below.
Or am I stuck with having the statement parsed each time I execute it?
Currently my INSERTs seem to be slow. In fact it's 15 times quicker to
write my data to a text file and then dump it to a MySQL table, so I'm
guessing I should be able to speed this up somehow? Cheers. TIA.
Duncan
>>> import MySQLdb
>>> conn = MySQLdb.connect(db='mytests')
>>> curs = conn.cursor()
>>> query = "INSERT INTO %s (%s) VALUES %s"
>>> tbl = 'AA_temp'
>>> vars = 'var1, var2, var3, var4, var5, var6, var7'
>>> vals = '("level1", "level1", "level1", "level1", "level1", "level1",
"level1")'
>>> curs.execute(query % (tbl, vars, vals))
1L
>>> query = "INSERT INTO ? (?) VALUES ?"
>>> curs.execute(query, (tbl, vars, vals))
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python22\Lib\site-packages\MySQLdb\cursors.py", line 70, in
execute
raise ProgrammingError, m.args[0]
ProgrammingError: not all arguments converted
>>>
More information about the Python-list
mailing list