Reputation: 387
I have build my app using PyQt6, MySQL database and MySQL connector. I have tested the connection and user privileges and every thing was fine. The app interacts with the database successfully when run from the python code using CMD. But once the app is bundled using pyinstaller, the app (exe) fails to open. I am unable to figure out why.
I designed a simple crud app to test and debug where the problem was. The crud app also works fine when run from the python script. But once the app is bundled using the pyinstaller, the app starts and displays the login page. Upon the inputting the username and password and clicking the login button, the app crashes without any error.
The main app doesn't even start after bundling while the crud app crashes after hitting the login button.
Here is the sample of the crud code:
import sys
import mysql.connector
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QMessageBox, QTableWidget, QTableWidgetItem, QHBoxLayout
# Create Database Connection
def create_connection():
try:
conn = mysql.connector.connect(
host="localhost",
user="crud_user",
password="12345",
database="crud_app",
port=3307
)
return conn
except mysql.connector.Error as err:
QMessageBox.critical(None, "Database Error", f"Failed to connect to database:\n{err}")
return None
# Login Page
class LoginPage(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Login")
self.setGeometry(100, 100, 300, 200)
layout = QVBoxLayout()
self.username_input = QLineEdit()
self.username_input.setPlaceholderText("Username")
self.password_input = QLineEdit()
self.password_input.setPlaceholderText("Password")
self.password_input.setEchoMode(QLineEdit.EchoMode.Password)
self.login_button = QPushButton("Login")
self.login_button.clicked.connect(self.login)
layout.addWidget(QLabel("Username:"))
layout.addWidget(self.username_input)
layout.addWidget(QLabel("Password:"))
layout.addWidget(self.password_input)
layout.addWidget(self.login_button)
self.setLayout(layout)
def login(self):
try:
username = self.username_input.text()
password = self.password_input.text()
conn = create_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE username=%s AND password=%s", (username, password))
user = cursor.fetchone()
conn.close()
if user:
self.crud_page = CRUDPage()
self.crud_page.show()
self.close()
else:
QMessageBox.warning(self, "Error", "Invalid username or password")
except Exception as err:
QMessageBox.critical(self, "Error", f"Login failed: {str(err)}")
# CRUD Page
class CRUDPage(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("CRUD App")
self.setGeometry(100, 100, 600, 400)
layout = QVBoxLayout()
self.table = QTableWidget()
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(["ID", "Name", "Age", "City"])
layout.addWidget(self.table)
self.load_data()
self.name_input = QLineEdit()
self.name_input.setPlaceholderText("Name")
self.age_input = QLineEdit()
self.age_input.setPlaceholderText("Age")
self.city_input = QLineEdit()
self.city_input.setPlaceholderText("City")
self.add_button = QPushButton("Add")
self.add_button.clicked.connect(self.add_record)
self.delete_button = QPushButton("Delete")
self.delete_button.clicked.connect(self.delete_record)
form_layout = QHBoxLayout()
form_layout.addWidget(self.name_input)
form_layout.addWidget(self.age_input)
form_layout.addWidget(self.city_input)
form_layout.addWidget(self.add_button)
form_layout.addWidget(self.delete_button)
layout.addLayout(form_layout)
self.setLayout(layout)
def load_data(self):
conn = create_connection()
cursor = conn.cursor()
cursor.execute("SELECT * FROM records")
rows = cursor.fetchall()
conn.close()
self.table.setRowCount(len(rows))
for row_idx, row_data in enumerate(rows):
for col_idx, data in enumerate(row_data):
self.table.setItem(row_idx, col_idx, QTableWidgetItem(str(data)))
def add_record(self):
name = self.name_input.text()
age = self.age_input.text()
city = self.city_input.text()
if not name or not age:
QMessageBox.warning(self, "Input Error", "Please enter name and age")
return
conn = create_connection()
cursor = conn.cursor()
cursor.execute("INSERT INTO records (name, age, city) VALUES (%s, %s, %s)", (name, age, city))
conn.commit()
conn.close()
self.load_data()
def delete_record(self):
selected = self.table.currentRow()
if selected == -1:
QMessageBox.warning(self, "Selection Error", "Please select a row to delete")
return
record_id = self.table.item(selected, 0).text()
conn = create_connection()
cursor = conn.cursor()
cursor.execute("DELETE FROM records WHERE id = %s", (record_id,))
conn.commit()
conn.close()
self.load_data()
# Run the App
app = QApplication(sys.argv)
login = LoginPage()
login.show()
sys.exit(app.exec())
Upvotes: -1
Views: 32