Pyqt6 macos connect to sql server freeze
-
I have a question.
System: macOS 15.0.1 (24A348)
PyQt Version: PyQt6 6.7.1
Database: SQL Server (MSSQL)
Issue: When I call db.opendb(), it freezes.Installation Method: I created a Python environment using Conda, installed Python 3.12, and then ran pip install PyQt6.
When I use PyQt6 to connect to my database, the code freezes without giving any feedback; it just hangs there. I also tried using PySide6 to run the application, but it freezes at the same point when attempting to open the database.
Has anyone else faced this issue?
Ironically, the exact same code runs flawlessly on Linux and Windows.
By the way, I am using regular pyodbc to connect to the database, and it works well.
import sys from PyQt6.QtSql import QSqlDatabase, QSqlQuery class DatabaseConnection: _instance = None def __new__(cls): if cls._instance is None: cls._instance = super().__new__(cls) cls._instance.db = None cls._instance._connect() return cls._instance def _connect(self): if self.db is None: self.db = QSqlDatabase.addDatabase("QODBC") self.db.setDatabaseName("mydsn") self.db.setUserName("sa") self.db.setPassword("xxxxxx") # self.db = QSqlDatabase("QSQLITE") # self.db.setDatabaseName("mydatabase.db") if not self.db.open(): error_message = self.db.lastError().text() print(f"Error: Unable to connect to the database! {error_message}") self.db = None else: print("Connected to the database successfully!") def get_connection(self): return self.db def close_connection(self): if self.db is not None: self.db.close() self.db = None print("Database connection closed.") def execute_query(self, query_string): if self.db is None or not self.db.isOpen(): self.db = self.get_connection() # print("Error: No active database connection.") # return None query = QSqlQuery(self.db) if not query.exec(query_string): error_message = query.lastError().text() print(f"Query execution failed: {error_message}") return None else: print("Query executed successfully.") return query
does anyone can help me , thank you
-
Hi,
Which version of PyQt6 ?
Which version of macOS ?
How did you install PyQt6 ?
Can you try with PySide6 to see if you have the same issue ? -
Hi,
Which version of PyQt6 ?
Which version of macOS ?
How did you install PyQt6 ?
Can you try with PySide6 to see if you have the same issue ? -
I don't think it's necessarily for that reason but having re-read your code, I see you are currently over engineering things. QSqlDatabase is already a singleton class and you should not keep a copy of the instance you are using as class member variable.
I would recommend writing a simple basic sample script that just does the QSqlDatabase creation and connection to ensure it's really that that is blocking.