0

can someone help me solve this problem?

I'm using Blender 2.74 and Python 3.4 with the correct connector to MySQL. By the way, I'm just a beginner in using Blender and Python. What I want is to make a login UI and save the inputted name into the database, but my code seems a bit off or totally wrong. When I run the code, it didn't save the value in the variable, but when i try to run it in python IDE it worked.

Here's the code:

import sys

sys.path.append('C:\Python34\Lib\sitepackages')
sys.path.append('C:\Python34\DLLs')

import mysql.connector

import bge
bge.render.showMouse(1) 

cont = bge.logic.getCurrentController()
own = cont.owner 

sensor = cont.sensors ["input"]

#this variable suppose to get the inputted name
pname = ""

db = mysql.connector.connect(user='root', password='', host='localhost',    database='database')

cursor = db.cursor()

add_player = ("INSERT INTO table " "(PlayerName) " "VALUES (%s)")
data_player = (pname)
cursor.execute(add_player, data_player)

#The 2nd one that i tried, and the same error
cursor.execute("INSERT INTO storymode" "(PlayerName)" "VALUES (%(pname)s)")

db.commit() 

db.close()

My questions are: Why it always have an error saying syntax error near the %s;Am I missing something in the code or do i need an add-on/s to make it work properly?Thank you very much for reading my post and for the people who will give their opinions.

natsu
  • 1
  • 1
  • I know neither Python nor Blender's interface to it, but I'm surprised that 'pname' would work without inverted commas. – Strawberry May 24 '15 at 08:15
  • I'm sorry, im just a newbie in python programming. what do you mean by inverted commas? are you referring to the variable ? – natsu May 24 '15 at 09:20
  • This -> ' <- is an inverted comma. – Strawberry May 24 '15 at 09:27
  • So that's what you mean by inverted comma. But, my problem is the code for inserting data, it seems that i'm missing something near the %s. But i will try the inverted comma to see if it can change my current problem. Thank you for giving your thoughts about this. – natsu May 25 '15 at 18:56
  • I tried the inverted comma in variable 'pname' = "Name" with name as a set value but a new error pops up: "SyntaxError: can't assign to literal". What does this mean? – natsu May 26 '15 at 06:14
  • possible duplicate of [Blender database using Python (errors)](http://stackoverflow.com/questions/30687155/blender-database-using-python-errors) – sambler Jun 07 '15 at 07:45
  • pname = 'Name' or "Name". Consider going through tutorials on how to assign to variables. As you are learning. Get the basics strong. http://www.tutorialspoint.com/python/python_variable_types.htm. http://www.learnpython.org/en/Variables_and_Types – nish Oct 24 '15 at 17:25

2 Answers2

0

prime facie,

cursor.execute("INSERT INTO storymode" "(PlayerName)" "VALUES (%(pname)s)")

should look like

cursor.execute("""INSERT INTO anooog1 VALUES (%s,%s)""",(188,90)) (ref: How can I insert data into a MySQL database?)

cursor.execute("""INSERT INTO storymode VALUES (%s)""", (pname))

Community
  • 1
  • 1
nish
  • 1,008
  • 4
  • 17
  • 34
0

It seems you didn't say what is the player name which needs to be substituted for %s or %(pname)s to be inserted to table.

add_player = ("INSERT INTO table " "(PlayerName) " "VALUES (%s)" % ('Nastu'))

or

add_player = ("""INSERT INTO table (PlayerName) VALUES (%s)""" % ('Nastu'))

Both the above mentioned is same one of them using quote thrice is multiline string in Python, otherwise what you have used is will concatenate the strings.

cursor.execute("INSERT INTO storymode" "(PlayerName)" "VALUES (%(pname)s)"  % {'pname' : 'nish'})

Please see through parameterized queries which is related to this

Does Python support MySQL prepared statements?

Python MySQL Parameterized Queries

Community
  • 1
  • 1
Shankar
  • 846
  • 8
  • 24