0

I'm using Blender 2.74 and Python 3.4 with the correct connector for 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 wrong. When I try to run the code, it didn't save the value in the variable, but when i try to run it in python IDE (PyCharm) it worked.

Here's the code:

import sys

sys.path.append('C:\Python34\Lib\site-packages')
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 ["enter"]
pname = own.get("prpText")

enter = cont.sensors ["enter"]
numpadenter = cont.sensors ["numpadenter"]

if enter.positive or numpadenter.positive:
    db = mysql.connector.connect(user='root', password='', host='localhost', database='dbname')

    cursor = db.cursor()

    #1st:
    cursor.execute("INSERT INTO tblname VALUE(%s", (var))

    #2nd:
    #cursor.execute("INSERT INTO storymode VALUES({})" .format(pname))

#this are the other codes that i have tried so far:
#add_player = ("INSERT INTO tblname " "(TblColumnName) " "VALUES (%s)")
#data_player = (var)
#cursor.execute(add_player, data_player)

#cursor.execute("INSERT INTO tblname" "(TblColumnName)" "VALUES (%(var)s)")

db.commit()

db.close()  

When I run the 1st code the error is: mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your mysql server version for the right syntax to use near '%s' at line 1.

When I run the second code: Unknown column 'data' in 'field list'

Can someone help me to fix this? or Do I need some add-ons in blender for it to work?

Thank you very much for reading my post and for the people who will give their opinions.

natsu
  • 1
  • 1
  • `"INSERT INTO tblname VALUE(%s"` ... there is a closing bracket missing. the whole line should probably read (not tested) `cursor.execute("INSERT INTO tblname VALUE(%s)" % var)` – hiro protagonist Jun 06 '15 at 22:05
  • 1
    sorry. this is python3.4; then: `cursor.execute("INSERT INTO tblname VALUE({})".format(var))` – hiro protagonist Jun 06 '15 at 22:13
  • I tried the closing bracket but there is also an error: invalid syntax – natsu Jun 06 '15 at 22:20
  • right, it should be `VALUES`; and maybe rather `cursor.execute("INSERT INTO tblname VALUES(%s)",var)`. compare with this post: https://stackoverflow.com/questions/5687718/how-can-i-insert-data-into-a-mysql-database – hiro protagonist Jun 06 '15 at 22:28
  • Do you know anything else? 'Cause i think i already tried that one. – natsu Jun 06 '15 at 23:57
  • can you `DESCRIBE` your table? do you give values for all columns in the table in your `INSERT` statement? – hiro protagonist Jun 07 '15 at 07:48
  • First asking the same question again doesn't help. Check the syntax of the sql statement not just the command that sends it. You show `insert into tblname` as well as `insert into storymode`. I also doubt the `(TblColumnName)` you have is right either. The table and column names need to match names used in the database which you aren't showing here. – sambler Jun 07 '15 at 07:59

0 Answers0