Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pyqt6 macos connect to sql server freeze
Forum Updated to NodeBB v4.3 + New Features

Pyqt6 macos connect to sql server freeze

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 401 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Karl
    wrote on last edited by Karl
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      0
      • SGaistS SGaist

        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 ?

        K Offline
        K Offline
        Karl
        wrote on last edited by
        #3

        @SGaist Hi. thank you for your response
        i have rewrite the question. as you mentioned, i have tried the PySide6, still faces same issue.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved