繁体   English   中英

Blender Python MySQL

[英]Blender Python MySQL

有人可以帮我解决这个问题吗?

我将Blender 2.74和Python 3.4与正确的MySQL连接器配合使用。 顺便说一下,我只是使用Blender和Python的初学者。 我想要的是创建一个登录UI并将输入的名称保存到数据库中,但是我的代码似乎有点不正确或完全错误。 当我运行代码时,它没有将值保存在变量中,但是当我尝试在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附近总是有错误提示语法错误;我是否在代码中遗漏了某些东西,或者我需要一个附加组件来使其正常工作?非常感谢您阅读我的文章和对于愿意发表意见的人们。

原始相

cursor.execute(“插入故事模式”“(PlayerName)”“ VALUES(%(pname)s)”)

应该看起来像

cursor.execute(“”“”插入到anooog1值(%s,%s)“”,(188,90))(参考: 如何将数据插入MySQL数据库?

cursor.execute(“”“”插入故事模式值(%s)“”“,(pname))

似乎您没有说要插入表中的%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'})

请查看与此相关的参数化查询

Python是否支持MySQL预备语句?

Python MySQL参数化查询

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM