[英]How to connect my Kivy code to MySql database?
I'm trying to create an application that takes login from user and sends data to mysql database but don't know how to edit my code for it.我正在尝试创建一个从用户那里获取登录信息并将数据发送到 mysql 数据库但不知道如何编辑我的代码的应用程序。 Need help with it.
需要帮助。 Also, what I'm trying to creating is an application for speech to text translation.
此外,我正在尝试创建一个用于语音到文本翻译的应用程序。 Currently my code is switching to the next screen but have to take speech input from user.
目前我的代码正在切换到下一个屏幕,但必须接受用户的语音输入。 How can I do it?
我该怎么做?
I've watched some tutorials but they are not applicable with my code我看过一些教程,但它们不适用于我的代码
.py file .py 文件
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.label import Label
Builder.load_string("""
""")
class Rootwidget(ScreenManager):
pass
class SigninWindow(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def validate_user(self):
user = self.ids.username_field
pwd = self.ids.pwd_field
info = self.ids.info
uname = user.text
passw = pwd.text
if uname == '' or passw == '':
info.text = '[color=#FF0000]username and/ or password required[/color]'
else:
if uname == 'admin' and passw == 'admin':
info.text = '[color=#00FF00]Logged In successfully![/color]'
App.get_running_app().root.current = "SignedIn"
else:
info.text = '[color=#FF0000]Invalid Username and/or Password[/color]'
class SigninApp(App):
def build(self):
return Rootwidget()
if __name__=="__main__":
sa = SigninApp()
sa.run()
.kv file .kv 文件
<Rootwidget>:
Screen:
name: "SignIn"
SigninWindow:
Screen:
name: "SignedIn"
Label:
text: "You have signed in."
<FlatButton@ButtonBehavior+Label>:
font_size: 16
<SigninWindow>:
id: main_win
orientation: "vertical"
spacing: 10
space_x: self.size[0]/3
canvas.before:
Color:
rgba: (1,1,1, 1)
Rectangle:
size: self.size
pos: self.pos
BorderImage:
source: 'E:\pythonpics/blu2.png'
pos: self.pos
size: self.size
BoxLayout:
size_hint_y: None
height: 50
canvas.before:
Color:
rgba: (.06, .45, .45, 1)
Rectangle:
size: self.size
pos: self.pos
Label:
text: "Hello! Dear User Please Sign In"
bold: True
size_hint_x: .9
BoxLayout:
orientation: 'vertical'
padding: main_win.space_x, 10
#spacing: 20
BoxLayout:
orientation: "vertical"
spacing: 10
size_hint_y: None
height: 100
Label:
id: info
text: ''
markup: True
size_hint_y: None
height: 20
TextInput:
id: username_field
hint_text: "Username"
multiline: False
focus: True
on_text_validate: pwd_field.focus = True
TextInput:
id: pwd_field
hint_text: "Password"
multiline: False
password: True
on_text_validate: root.validate_user()
Label:
id: sp
size_hint_y: None
height: 40
Button:
text: "Sign In"
size_hint_y: None
height: 40
background_color: (.06,.45,.45, 1)
background_normal: ''
on_release: root.validate_user()
Label:
id: sp2
The code should be able to take user data and save in the mysql database代码应该能够获取用户数据并保存在mysql数据库中
First install mysql-connector with pip in linux首先在linux中用pip安装mysql-connector
pip install mysql-connector
Then import that library然后导入该库
import mysql.connector
Later create a dictionary with your database credentials like this稍后使用您的数据库凭据创建一个字典,如下所示
config = {'user': 'insert_user',
'password': 'insert_password',
'host': '000.000.000.000',
'database': 'insert_database_name',
'raise_on_warnings': True}
Then create a class that has all the queries and structure to connect mysql databases然后创建一个具有连接 mysql 数据库的所有查询和结构的类
class mydatabase:
def __init__(self):
self.db = mysql.connector.connect(**config)
self.c = self.db.cursor()
def get_rows(self):
# a,b,c,d depence of your database structure and tables
# use the query example below, query needs parenthesis obligated
query=('SELECT {} FROM {} WHERE({}='{}')'.format("a","b","c","d"))
self.c.execute(query)
return self.c.fetchall()
And Finally, run this script anywhere to run a query.最后,在任何地方运行此脚本以运行查询。
db_connection=mydatabase()
db_answer=db_connection.get_rows()
Notes: Adapted from el3ien because he's using other database library注释:改编自el3ien因为他正在使用其他数据库库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.