有人能帮我解决这个问题吗?
我正在使用Blender2.74和Python3.4,并使用正确的连接器连接到MySQL。顺便说一下,我只是使用Blender和Python的初学者。我想要的是做一个登录用户界面,并将输入的姓名保存到数据库中,但我的代码似乎有点不对劲或完全错误。当我运行代码时,它没有将值保存在变量中,但当我尝试在python IDE中运行它时,它工作了。
代码如下:
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()我的问题是:为什么它总是在%s附近出现语法错误的错误;我是不是在代码中遗漏了什么,或者我需要一个附加组件才能使它正常工作?非常感谢您阅读我的帖子,感谢那些提出意见的人。
发布于 2015-10-25 01:21:21
表面上看,
Cursor.execute(“插入到故事模式”"(PlayerName)“”值(%(pname)s)")
应该看起来像
Cursor.execute(“INSERT INTO anooog1 VALUES (%s,%s)”,(188,90)) (参考:How can I insert data into a MySQL database?)
Cursor.execute(“插入到故事模式值(%s)”,(pname))
发布于 2015-10-25 01:33:49
您似乎没有说要插入到表中的%s或%(pname)s需要替换的球员名称是什么。
add_player = ("INSERT INTO table " "(PlayerName) " "VALUES (%s)" % ('Nastu'))
or
add_player = ("""INSERT INTO table (PlayerName) VALUES (%s)""" % ('Nastu'))上面提到的两个都是一样的,其中一个在Python中使用引号三次是多行字符串,否则你使用的是连接字符串。
cursor.execute("INSERT INTO storymode" "(PlayerName)" "VALUES (%(pname)s)" % {'pname' : 'nish'})请查看与此相关的参数化查询
https://stackoverflow.com/questions/30421372
复制相似问题