使用 Kivy 检索 MySQL

Retrieving MySQL with Kivy(使用 Kivy 检索 MySQL)
本文介绍了使用 Kivy 检索 MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Kivy code, where the output is:

我想用从 MySQL 检索到的字符串替换 Box No.

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

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

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

但这没有用.

问:如何在 kv 文件中进行 MySQL 查询和打印单个字符串.

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

这篇关于使用 Kivy 检索 MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)