ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: laptop - Mandriva2008 Free / XP Pro sp2 | Desktop - XP MC2k5 sp2
Posts: 28
Rep:
Python MySQLdb syntax issues...
Im very new to SQL in general, let alone coding it into python. I can interact with a MySQL database just fine for the most part, but im running into some problems here... This is the function in my script that keeps raising errors:
Code:
def NewChar():
""" NewChar() -
Call this function to begin new character generation.
At this time it is the only function you need to call from the NewCharacter module.
It takes no arguments.
"""
CharAccount = NewAccount()
CharName = raw_input("Enter New Characters Name: \n")
CharGender = NewGender()
CharJob = NewJob()
CharLevel = "1"
Attributes = GetAtt() ###--- imports the attributes to be added to character info
Strength = Attributes[0]
Dexterity = Attributes[1]
Inteligence = Attributes[2]
Charm = Attributes[3]
Luck = Attributes[4]
###--- This will print the results to a script that will be used to store
###--- and retrieve character data for use in the game. It will eventually
###--- be phased out by a database or encrypted file and calling scripts so
###--- it wont be accessable by the user.
#AppendChar = '\n["' + CharName + '", "' + CharGender + '", "' + CharJob + '", ' + CharLevel + ', ' + Strength + ', ' + Dexterity + ', ' + Inteligence + ', ' + Charm + ', ' + Luck + ']'
#CharSheet = "Character.hro"
#CharOutput = open(CharSheet, 'a', 5000)
#CharOutput.writelines(AppendChar)
#CharOutput.close()
###--- MySQL implementation of the new character save.
conn = MySQLdb.connect(host = 'localhost', user = 'hero', passwd = 'password', db = 'hero')
cursor = conn.cursor()
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)
CharInfo = (CharAccount, CharName, CharGender, CharJob, CharLevel, Strength, Dexterity, Inteligence, Charm, Luck)
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))
cursor.execute("SELECT name, job FROM %s" % CharAccount)
while (1):
row = cursor.fetchone()
if row == None:
break
print "\n \n \n You just created: %s \n Job class is: %s" % (row[0], row[1])
cursor.close()
conn.commit()
conn.close()
print "\n \n \n Your character is made!"
MakeAgain = raw_input("\n \n \n Would you like to make another character while we are at it? (y, n): \n")
MakeAgain = MakeAgain.lower()
MakeAgain = MakeAgain[0]
if MakeAgain == "y":
NewChar()
else:
return
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... I have reworked the last line so many times its pathetic and have seen about 3 or 4 different errors, but they are almost all being raised by the same functions within the MySQLdb code. All variables i am attempting to assign are in string form and have worked in a test script i made that does everything the same except substituting the strings within my query. Ive been stuck on this all week and have read numerous tutorials, the DB-API specification sheet, the MySQL manual, the MySQLdb documentation, and a few books... none of which seem to adress my problem since they are all only assigning variables to the table name OR the values of the query, not both. Please help me figure this out.
I'm guessing, because I'm not familiar with Python or cursors, but I notice that the INSERT INTO has nine items, the VALUES have nine items, but the following line has 10. beginning with CharAccount. I'm not exactly sure what CharAccount is, but I accept that it seems to be somehow tied up as part of the cursor.execute syntax.
Can you explain that for us, unless someone comes along and gives you the answer first of course.
you would think that would work, but it sadly doesnt.... that was the first way i tried... someone helped me last night though from the python mailing list, so i found a solution...
it was such a simple idea that i didnt even think of it... just escape the % and i can use both string formatting AND the mysqldb argument declaration...
I'm guessing, because I'm not familiar with Python or cursors, but I notice that the INSERT INTO has nine items, the VALUES have nine items, but the following line has 10. beginning with CharAccount. I'm not exactly sure what CharAccount is, but I accept that it seems to be somehow tied up as part of the cursor.execute syntax.
Can you explain that for us, unless someone comes along and gives you the answer first of course.
no problem, CharAccount was supposed to be inserted in place of the table name... "INSERT INTO %s" ... thats where it goes
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.