Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Kivy检索MySQL_Mysql_Kivy_Kivy Language - Fatal编程技术网

用Kivy检索MySQL

用Kivy检索MySQL,mysql,kivy,kivy-language,Mysql,Kivy,Kivy Language,我有一个Kivy代码,其中输出为: 我想用从MySQL中检索到的字符串替换框号 到目前为止,我已经尝试在python脚本中实现MySQL: class RemoveScreen(MyLayout): def __init__(self,**kwargs): db = MySQLdb.connect("localhost", "root", "[PASSWORD]", "tcs_microrage_

我有一个Kivy代码,其中输出为:

我想用从MySQL中检索到的字符串替换
框号

到目前为止,我已经尝试在python脚本中实现MySQL:

class RemoveScreen(MyLayout):


    def __init__(self,**kwargs):
        db = MySQLdb.connect("localhost", "root", "[PASSWORD]", "tcs_microrage_crm")
        cursor=db.cursor()
        self.var = StringVar()
        self.label1 = Label(self, text=0, textvariable=self.var)
        myvar=str(self.var)
        #http://stackoverflow.com/questions/775296/python-mysql-parameterized-queries
        cursor.execute("SELECT part_name FROM stock_lists WHERE part_number = %s", (myvar))
        self.myvar=StringVar()
        self.myvar.set(cursor.fetchone())
        self.label2 = Label(self, text=0, textvariable=myvar)
但这不起作用


Q:如何进行MySQL查询并在kv文件中打印单个字符串。

为了向您展示如何进行查询,我做了一个小的搜索示例。
这将在数据库中搜索水果名称,并将其名称和价格输出到表中

从kivy.app导入应用
导入MySQLdb
从kivy.uix.boxlayout导入boxlayout
从kivy.uix.gridlayout导入gridlayout
从kivy.uix.label导入标签
从kivy.uix.button导入按钮
从kivy.uix.textinput导入textinput
DbCon类:
定义初始化(自):
self.db=MySQLdb.connect(user=“root”,passwd=“pw”,db=“kivy”)
self.c=self.db.cursor()
def get_行(self,search=”“):
self.c.execute(“从名为REGEXP.*%s.*”的水果中选择*限制3”%search)
返回self.c.fetchall()
课程表(BoxLayout):
定义初始(自我,**kwargs):
超级(表,自).\uuuu初始化(**kwargs)
self.orientation=“垂直”
self.search\u field=BoxLayout(方向=“水平”)
self.search\u input=textfinput(text='search',multiline=False)
self.search\u按钮=按钮(text=“search”,打开时按=self.search)
self.search\u字段.add\u小部件(self.search\u输入)
self.search\u字段。添加\u小部件(self.search\u按钮)
self.add\u小部件(self.search\u字段)
self.add_小部件(标签(text=“table”))
self.table=GridLayout(cols=2,rows=4)
self.table.add_小部件(标签(text=“Fruit”))
self.table.add_小部件(标签(text=“Price”))
self.rows=[[Label(text=“item”)、Label(text=“price”)],
[标签(text=“item”)、标签(text=“price”)],
[标签(text=“item”)、标签(text=“price”)]]
对于项目,self.rows中的价格:
self.table.add_小部件(项)
self.table.add_小部件(价格)
self.add_小部件(self.table)
self.db=DbCon()
self.update_表()
def更新_表(self,search=”“):
对于索引,枚举中的行(self.db.get_rows(search)):
self.rows[index][0]。text=行[1]
self.rows[index][1].text=str(第[2]行)
def清除表(自身):
对于范围(3)中的索引:
self.rows[index][0]。text=“”
self.rows[index][1]。text=“”
def搜索(self,*args):
self.clear_表()
self.update\u表(self.search\u input.text)
类别MyApp(应用程序):
def生成(自):
返回表()
MyApp().run()