MySQLdb syntax issues - HELP

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Dec 16 16:45:44 EST 2007


On 16 dic, 17:48, Luke <elven... at msn.com> wrote:
> Bruno Desthuilliers wrote:

>> Err... Are you sure you want a new table here ?
>
> 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)

re???? No. Just plain SQL, see below. As you wouldn't -in general-
define a new class for each character, the same happens for the
tables: you wouldn't create a new table for each character, as they
all share the same set of attributes; just insert a new *row* of data
into the table. BTW, it's similar to what you appear to be doing
previously: appending a new row to Character.hro file, conceptually
it's the same thing.
Table names and column names are usually known when you define the
application and remain fixed. And they can't be provided as parameters
in the execute method: that's why Bruno split it in two steps,
building a sql statement first (with the table name in it) and then
executing it with the remaining parameters.
So, I would create the table ONCE, with name Character, and a new
column Account.

To insert a new character:
sql = """
INSERT INTO Character (account, name, gender, job, level, str, dex,
intel, cha, luc)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
cursor.execute(sql, (CharAccount, CharName, CharGender, CharJob,
CharLevel,
Strength, Dexterity, Inteligence, Charm, Luck))

Retrieving a certain character is not harder:
sql = """
SELECT name, gender, job, level, str, dex, intel, cha, luc
FROM Character
WHERE account = %s"""
cursor.execute(sql, (CharAccount,))
CharName, CharGender, CharJob, CharLevel, Strength, Dexterity,
Inteligence, Charm, Luck = cursor.fetchone()

--
Gabriel Genellina



More information about the Python-list mailing list