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.