Error conecting SQLOLEDB with QtSQL / QSqlDatabase

131 Views Asked by At

I'm struggling to get a connection using QtSql. The problem is that my type of connection is a "SQLOLEDB", and I can only find ODBDC examples... Please any help would be so much appriciated! Thanks in advice!

    connection_string = ("Provider=SQLOLEDB;Data Source=XXXX;Initial Catalog=XXXX;User Id=XXXX;Password=XXXX")
db = QSqlDatabase().addDatabase("???", connection_string)
db.open()

Should I use this template?

db = QSqlDatabase.addDatabase("QODBC")
        db.setHostName("XXXX")
        db.setPort(1433)
        db.setDatabaseName("XXXX")
        db.setUserName("XXXX")
        db.setPassword("XXXX")
        ok = db.open()
1

There are 1 best solutions below

0
Marçal Xena On

Finally I've managed to connect to the server using a DSN like this:

db32 = QSqlDatabase.addDatabase("QODBC")
        db32.setHostName("yourServerHost")
        db32.setDatabaseName("dsnnameinODBCWindowsManager")
        db32.setUserName("XXXX")
        db32.setPassword("XXXX")
        ok32 = db32.open()

Before you must configure a ODBC DSN in your PC using ODBC Windows Manager, and get it to connect to your database. Comments:

  1. Make sure the architecture of your DSN matches your Python (32 or 64 bit).
  2. Repeat the User and Password info when creating the DSN in ODBC Windows Manager and when connecting when using QtSql as shown.

Hope it helps anyone!