如何将Kivy输入存储到数据库中?

时间:2018-08-03 21:49:21

标签: python python-3.x kivy kivy-language

我是15岁的学生,他正在尝试创建一个应用程序,该应用程序可以存储我的作业并在每天必须提交作业以提醒我之前一天向我发送邮件。

现在,在简短介绍之后,我在尝试开发该应用程序时遇到了一个问题。我想将用户输入存储到数据库中(我正在使用Xampp,我已经很熟悉了)。我还必须补充一点,该应用程序将在我的Android手机中使用。一方面,我设法创建了输入,另一方面,当我通过文本使用Python时,我也知道如何通过Phpmysql存储变量(输入)。

现在问题出在这里,我在使用Kivy时不知道如何存储它。我已经花了几个小时试图找到答案,但我一直无法弄清楚。我想补充一点,我已经看到了一种称为Kv语言的语言,可以轻松地用Python进行编码,但是后来我不知道如何将其连接到当前代码。

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.config import Config
from kivy.uix.textinput import TextInput
from time import strftime

# Estableix mesures per l'app de mòbil
Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '670')

# Aspecte i elements de cada pantalla
Builder.load_string("""
<Inici>:
    BoxLayout:
        Button:
            text: '+Deures'
            font_size: 20
            background_disabled_normal: ''
            background_normal: ''
            background_color: .93, .93, .2, .9
            on_press: root.manager.current = 'Afegirdeures'
        Button:
            text: '-Deures'
            font_size: 20
            background_normal: ''
            background_color: 1, .3, .4 , .85
            on_press: root.manager.current = 'Eliminardeures'
        Button:
            text: 'Consulta'
            font_size: 20
            background_normal: ''
            background_color: .93, .93, .2, .9
            on_press: root.manager.current = 'Consultar'
        Button:
            text: 'Sortir'
            font_size: 20
            background_normal: ''
            background_color: 1, .3, .4 , .85
            on_press: DeuresManager().stop()

<Afegirdeures>:
    BoxLayout:
        orientation: 'horizontal'
        BoxLayout:
            orientation: 'vertical'
            Label:
                text: 'Introdueix el dia'
            TextInput:
                text: 'Format: DD/MM/AA'
            Label:
                text: 'Introdueix la tasca'
            TextInput:
                text: 'Tasca...'
            Button:
                text: 'Executa'
                on_press: 
        Button:
            text: 'Tornar al menú'
            font_size: 20
            background_normal: ''
            background_color: 1, .3, .4 , .85
            on_press: root.manager.current = 'Menú'

<Eliminardeures>:
    BoxLayout:
        Button:
            text: 'My settings button'
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'Menú'

<Consultar>:
    BoxLayout:
        Button:
            text: 'My settings button'
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'Menú'
""")

# Declaració diferents pantalles
class Inici(Screen):
    pass

class Afegirdeures(Screen):
    pass

class Eliminardeures(Screen):
    pass

class Consultar(Screen):
    pass

# Administrador de pantalles
sm = ScreenManager()
sm.add_widget(Inici(name='Menú'))
sm.add_widget(Afegirdeures(name='Afegirdeures'))
sm.add_widget(Eliminardeures(name='Eliminardeures'))
sm.add_widget(Consultar(name='Consultar'))

# Executar aplicació
class DeuresManager(App):

    def build(self):
        return sm

if __name__ == '__main__':
    DeuresManager().run()

关于与数据库的连接以及变量的存储,我倾向于使用this-。

import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             db='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

try:
    with connection.cursor() as cursor:
        # Create a new record
        sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
        cursor.execute(sql, ('webmaster@python.org', 'very-secret'))

    # connection is not autocommit by default. So you must commit to save
    # your changes.
    connection.commit()

    with connection.cursor() as cursor:
        # Read a single record
        sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
        cursor.execute(sql, ('webmaster@python.org',))
        result = cursor.fetchone()
        print(result)
finally:
    connection.close()

如果您能帮助我,我将非常感激。 (如果可能,请不要发送指向文档的链接,因为我已经检查过它,但没有成功)。

PD:我已经检查了一些东西,但是我不明白为什么我的代码风格与此不同(https://kivy.org/docs/api-kivy.uix.textinput.html?highlight=input#module-kivy.uix.textinput

0 个答案:

没有答案