kivy mysql,使用Kivy检索MySQL

kivy mysql,使用Kivy检索MySQLIhaveaKivyco wheretheoutp Iwanttogetre withstringsr classRemoveS MyLayout

kivy mysql,使用Kivy检索MySQL

I have a Kivy code, where the output is:

gzYBj.png

I want to get replace the Box No. with strings retrieved from MySQL

So far I have tried to implement the MySQL to the python script:

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//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)

But this didn’t work.

Q: How can I do MySQL queries and print individual strings in the kv file.

解决方案

To show you how you could do that, I made a little search example.

This searches for fruit names in the database, and will output its name and price to the table.

from kivy.app import App

import MySQLdb

from kivy.uix.boxlayout import BoxLayout

from kivy.uix.gridlayout import GridLayout

from kivy.uix.label import Label

from kivy.uix.button import Button

from kivy.uix.textinput import TextInput

class DbCon:

def __init__(self):

self.db = MySQLdb.connect(user=”root”,passwd=”pw”,db=”kivy”)

self.c = self.db.cursor()

def get_rows(self,search = “”):

self.c.execute(“SELECT * FROM fruit WHERE name REGEXP ‘.*%s.*’ LIMIT 3” % search)

return self.c.fetchall()

class Table(BoxLayout):

def __init__(self,kwargs):

super(Table,self).__init__(kwargs)

self.orientation = “vertical”

self.search_field = BoxLayout(orientation=”horizontal”)

self.search_input = TextInput(text=’search’,multiline=False)

self.search_button = Button(text=”search”,on_press=self.search)

self.search_field.add_widget(self.search_input)

self.search_field.add_widget(self.search_button)

self.add_widget(self.search_field)

self.add_widget(Label(text=”table”))

self.table = GridLayout(cols=2,rows=4)

self.table.add_widget(Label(text=”Fruit”))

self.table.add_widget(Label(text=”Price”))

self.rows = [[Label(text=”item”),Label(text=”price”)],

[Label(text=”item”),Label(text=”price”)],

[Label(text=”item”),Label(text=”price”)]]

for item,price in self.rows:

self.table.add_widget(item)

self.table.add_widget(price)

self.add_widget(self.table)

self.db = DbCon()

self.update_table()

def update_table(self,search=””):

for index,row in enumerate(self.db.get_rows(search)):

self.rows[index][0].text = row[1]

self.rows[index][1].text = str(row[2])

def clear_table(self):

for index in range(3):

self.rows[index][0].text = “”

self.rows[index][1].text = “”

def search(self, *args):

self.clear_table()

self.update_table(self.search_input.text)

class MyApp(App):

def build(self):

return Table()

MyApp().run()

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/223475.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午2:07
下一篇 2026年3月17日 下午2:07


相关推荐

  • ADO.NET基础

    ADO.NET基础ADO.NET基础

    2022年4月24日
    40
  • Inside IIS & Asp.Net

    Inside IIS & Asp.Net

    2021年8月10日
    54
  • android换机备份,安卓手机备份迁移指南

    android换机备份,安卓手机备份迁移指南原标题:安卓手机备份迁移指南买了新手机,本来是一件非常开心的事情,但是如何将旧手机上的有用信息转移到新手机上,这可是一件让大家很犯愁的事情,今天就跟着小编一起来看看安卓手机的四种转移数据的方法吧。1、一键换机现在很多的手机都有一键换机的功能,能直接将旧手机上的所有数据转移到新手机上,非常的方便。下面我们用小米手机来看看具体的换机操作。首先在小米手机“设置—更多设置”中,找到“一键换机”功能。然后…

    2022年5月22日
    76
  • python生成可执行文件linux_python运行exe程序

    python生成可执行文件linux_python运行exe程序Python生成可执行文件exe一、安装pyinstallerpipinstallpyinstaller二、使用pyinstaller命令使用示例相对路径在程序目录中,运行命令pyinstallermyscript.py则可以在当前目录生成两个文件夹dist和build,exe文件在dist文件夹中。绝对路径在程序目录中,运行命令pyinstallerC:\mys…

    2022年5月3日
    159
  • Java API 1.8 中文版 免费下载

    Java API 1.8 中文版 免费下载JavaAPI1.8中文版免费下载无意中淘到的希望对大家有帮助!在这里免费分享给大家百度云盘链接提取码y6wo

    2022年7月26日
    13
  • 一文认识PYNQ

    零、前言PYNQ可以认为是Python+ZYNQ,但不是简单的相加。在使用上,可以说PYNQ开发是ZYNQ开发的集大成,也可以说PYNQ是ZYNQ的全栈式开发,里面涉及到的内容不仅包括FPGA设计、PS与PL的协同交互、HLS、linux驱动开发,而且还要熟悉Python开发并且使用Python各种库。PYNQ是Xilinx推出的一个开源项目,目的是使用Python开发Xilinx平台更加容易。使用Python语言和库,设计人员可以利用Xilin

    2022年4月5日
    40

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号