How to insert python/kivy input variable into MySQL database

  Kiến thức lập trình

I am having trouble getting a python/kivy varialbe input to load into my MySQL database. When I run the program, there is a kivy GUI input box that opens and asks the user to enter an id, email, and password.

After I enter the requested data, I want to be able to hit the “Submit” button and have those 3 input data fields be populated into my MySQL database into a table called Person. I have already established that Table and put 3 fields into it called “user_name”, “email”, and “password”. There is also a 4th field that auto assigns an auto incremented key, but that is done automatically by MySQL. I know the “Submit” button works properly and I can even print out the inputed data.

The code I am using to have this data submitted into database is mycursor.execute(“INSERT INTO Person(user_name, email, password) VALUES(%s,%s,%s)”,({f”id”}, {f”email”}, {f”password”})). I get an error on this stating _mysql_connector.MySQLInterfaceError: Python type set cannot be converted.

Here is the total code I have written:

import mysql.connector
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button

db = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="root",
    database="testdatabase"
    )

mycursor=db.cursor()

class MyGridLayout(GridLayout):
    # Intialize infinite keywords
    def __init__(self, **kwargs):
        #Call grid layout contstructor
        super(MyGridLayout, self).__init__(**kwargs)

        #Set columns
        self.cols = 1

        #create second grid layout
        self.top_grid = GridLayout()
        self.top_grid.cols = 2

        #add widgets
        self.top_grid.add_widget(Label(text="Choose Your User ID: "))
        #add input box
        self.id = TextInput(multiline=False)
        self.top_grid.add_widget(self.id)

        #add widgets
        self.top_grid.add_widget(Label(text="Enter a Valid Email Address: "))
        #add input box
        self.email = TextInput(multiline=False)
        self.top_grid.add_widget(self.email)

        #add widgets
        self.top_grid.add_widget(Label(text="Choose a Strong Password: "))
        #add input box
        self.password = TextInput(multiline=False)
        self.top_grid.add_widget(self.password)

        #add top grid to app
        self.add_widget(self.top_grid)

        #create submit button
        self.submit = Button(text="Submit", font_size=32,)
        #bind button
        self.submit.bind(on_press=self.press)
        self.add_widget(self.submit)


    def press(self, instance):
        id = self.id.text
        email = self.email.text
        password = self.password.text


        self.add_widget(Label(text=f"The User ID you entered is: {id} nThe email you entered is: {email} nThe password you entered is: {password}"))

        #clear the input boxes
        self.id.text = ""
        self.email.text = ""
        self.password.text = ""

mycursor.execute("INSERT INTO Person(user_name, email, password) VALUES(%s,%s,%s)",({f"id"}, {f"email"}, {f"password"}))


class MyApp(App):
    def build(self):
        return MyGridLayout()

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

Can somebody let me know what I am doing wrong here and point me to the solution of getting that variable input data to populate into my MySQL database.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT