0

I've made a kivy app using a MySQL database, it consists in three menus, one that lets you register, other that lets you enter if you enter a registered user and the final one that shows you the main app. Here is the .py file where I connect to the server:

class LoginWindow(Screen):
    first_namee = ObjectProperty(None)
    password = ObjectProperty(None)

    def loginBtn(self):
        q1 = """
        SELECT tablename.firstcolumn, tablename.secondcolumn
        FROM tablename
        """
        connection = create_db_connection("localhost", "yourusername", "yourpassword", "yourdbname")
        results = read_query(connection, q1)
        str = ''
        for result in results:
            str = str , result
        res = json.dumps(str)
        if self.first_namee.text in res:
            if self.password.text in res:
                self.reset()
                sm.current = "main"
            else:
                invalidLogin()
        else:
            invalidLogin()

create_db_connection() is called with this:

def create_db_connection(host_name, user_name, user_password, db_name):
    connection = None
    try:
        connection = mysql.connector.connect(
            host = host_name,
            user = user_name,
            passwd = user_password,
            database = db_name
        )
        print("MySQL Database connection succesful")
    except Error as err:
        print(f"Error: '{err}'")
    return connection

My .kv file is:


<LoginWindow>:
    name: "login"

    firstcolumn: firstcolumn
    secondcolumn: secondcolumn

    FloatLayout:
        Label:
            pos_hint: {"x":0.1, "top":1}
            size_hint: 0.1, 0.1
            text: "First name: "

        TextInput:
            pos_hint: {'center_x': 0.5, 'center_y': 0.9}
            size_hint: 0.4, 0.12
            id: firstcolumn
            multiline: False

        Label:
            pos_hint: {"x":0.1, "top":0.7}
            size_hint: 0.1, 0.1
            text: "Password: "

        TextInput:
            pos_hint: {'center_x': 0.5, 'center_y': 0.7}
            size_hint: 0.4, 0.12
            id: secondcolumn
            multiline: False

        Button:
            pos_hint: {"x":0.5, "y":0.13}
            size_hint: 0.1, 0.1
            text: "Login"
            on_release:
                root.manager.transition.direction = "up"
                root.loginBtn()

In my buildozer.spec I've enabled Internet permission and my requirements are the followed:

python3,kivy==2.0.0,mysql.connector,mysql.connector.python,Timer

Once I enter the registered user in my android, the app crashes and this error appears in the terminal of my virtualbox:

Error: '2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)'
AttributeError: 'NoneType' object has no attribute 'cursor'

I don't know what to do since the code runs well in my computer, something happens when I run it in my Android, can someone help? I'm a bit of a newbie.

Ded Meme
  • 1
  • 1
  • have you checked if the server is running at all? – nbk Nov 05 '21 at 18:09
  • Do you really have a mysql server running on your android device? Maybe you are trying to connect to the server on your computer? Remember localhost is network speak for "me" so the code as written is trying to connect to a database server on the same device that the code is running on. – AllAboutMike Nov 10 '21 at 00:47
  • @nbk it is running in my laptop. – Ded Meme Nov 10 '21 at 02:03
  • @AllAboutMike no, mysql server is running on my laptop. How do I change it so I connect to the server that is working on another device? – Ded Meme Nov 10 '21 at 02:05
  • you have tpo change to my.conf/ini to allow acess from outside, then you change te grants of the user and of course you need to let the firewall know that it can let connction to the port 4406 in, but when you use your laptop outside your safe envorpoment, disable the port or shut mysql down – nbk Nov 10 '21 at 09:23
  • The other option (if you actually want something that works on the android device) is to run sqlite. Python has built in support and it's still an SQL database. I've used it successfully. Of course if you intend this to be using a central/multiuser database server, then do what nbk said and access it there once you give yourself permission. – AllAboutMike Nov 20 '21 at 07:34

0 Answers0