MySQLdb syntax issues - HELP

Luke elvenson at msn.com
Sun Dec 16 15:48:45 EST 2007


Bruno Desthuilliers wrote:

> Luke a écrit :
 (snip)   
>>     cursor.execute("""
>>         CREATE TABLE %s
>>         (
>>          name     CHAR(40),
>>          gender   CHAR(40),
>>          job      CHAR(40),
>>          level    TEXT,
>>          str      TEXT,
>>          dex      TEXT,
>>          intel    TEXT,
>>          cha      TEXT,
>>          luc      TEXT
>>         )
>>     """ % CharAccount)
> 
> Err... Are you sure you want a new table here ?
 (snip)

yes, thats the easier way i can think of for now since i am so new to SQL,
eventually im sure i will put all the characters into one larger table
though... but for now i just dont feal like figuring out how to scan the
table for the records i need based on name of character... ill save that
for later. (unless there is a very easy way to do it that doesnt require
re)

 (snip)
>> The part that keeps getting me errors is:
>> 
>> -------------------------------------------------------
>> 
>>     cursor.execute("""
>>        INSERT INTO %s (name, gender, job, level, str, dex, intel, cha,
>>        luc) VALUES
>>         (%s, %s, %s, %s, %s, %s, %s, %s, %s)
>>     """, (CharAccount, CharName, CharGender, CharJob, CharLevel,
>>     Strength,
>> Dexterity, Inteligence, Charm, Luck))
>> 
>> -------------------------------------------------------
>> 
>> i have tried various ways to do this string insertion, and i keep getting
>> errors. it seems that MySQLdb doesnt like me assigning a string to the
>> table name if im going to assign more to values...
> 
> Your problem comes from confusion between Python's string formating
> system and db-api query arguments system - which sometimes (as here) use
> the same placeholder mark.
> 
> What you want here is:
> 
> sql = """
> INSERT INTO %s (name, gender, job, level, str, dex, intel, cha, luc)
> VALUES (%%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s, %%s)
> """ % CharAccount
> 
> cursor.execute(sql,  (CharName, CharGender, CharJob, CharLevel,
> Strength, Dexterity, Inteligence, Charm, Luck))

wow, i was so focused on learning the MySQLdb module i have been overlooking
simply escaping my % signs the whole time... nice catch, thanks alot. it
works like a charm now.


PROBLEM SOLVED, BUT IF YOU WANT TO ADD ANYTHING, FEEL FREE...



More information about the Python-list mailing list