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. Connection string for ODBC / Microsoft SQL Express
Forum Updated to NodeBB v4.3 + New Features

Connection string for ODBC / Microsoft SQL Express

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.9k Views 4 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.
  • M Offline
    M Offline
    Mark81
    wrote on last edited by
    #1

    I need to connect to a Microsoft SQL Express database through ODBC.
    I have got this information:

    • ip address of the server
    • name of the database
    • username
    • password

    Here my code:

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setConnectOptions();
    
    QString connectionString = QString("DRIVER={SQL Server};Server=192.168.2.22;Database=NAME;Uid=USER;Pwd=PASSWORD;");
    db.setDatabaseName(connectionString);
    qDebug() << db.open();
    qDebug() << db.lastError();
    

    The output is:

    false
    QSqlError("0", "QODBC: Unable to connect", "[unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found")

    It seems anything I put inside { } is the library it tries to open.
    So I tried to pass the full path of the driver - even I've never seen such a connection string:

    QString connectionString = QString("DRIVER={/home/user/bin/plugins/sqldrivers/libqsqlodbc.so};[...]");
    

    Now the error is different:

    QSqlError("0", "QODBC: Unable to connect", "[unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed")

    Before begin to dig this error, I'd like to be sure my approach is correct...

    artwawA 1 Reply Last reply
    0
    • M Mark81

      I need to connect to a Microsoft SQL Express database through ODBC.
      I have got this information:

      • ip address of the server
      • name of the database
      • username
      • password

      Here my code:

      QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
      db.setConnectOptions();
      
      QString connectionString = QString("DRIVER={SQL Server};Server=192.168.2.22;Database=NAME;Uid=USER;Pwd=PASSWORD;");
      db.setDatabaseName(connectionString);
      qDebug() << db.open();
      qDebug() << db.lastError();
      

      The output is:

      false
      QSqlError("0", "QODBC: Unable to connect", "[unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found")

      It seems anything I put inside { } is the library it tries to open.
      So I tried to pass the full path of the driver - even I've never seen such a connection string:

      QString connectionString = QString("DRIVER={/home/user/bin/plugins/sqldrivers/libqsqlodbc.so};[...]");
      

      Now the error is different:

      QSqlError("0", "QODBC: Unable to connect", "[unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed")

      Before begin to dig this error, I'd like to be sure my approach is correct...

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      @Mark81 According to the documentation you form the string wrongly.

      For more information please re-read.

      Kind Regards,
      Artur

      M 1 Reply Last reply
      1
      • artwawA artwaw

        @Mark81 According to the documentation you form the string wrongly.

        M Offline
        M Offline
        Mark81
        wrote on last edited by
        #3

        @artwaw I read that page but I thought that example was specific for HANA db!
        Anyway I think I have to select the proper library, in my case from:

        $ ldd libqsqlodbc.so 
        	linux-vdso.so.1 (0x00007ffeb81d5000)
        	libodbc.so.2 => /lib/x86_64-linux-gnu/libodbc.so.2 (0x00007fbfe2cb1000)
        	libQt6Sql.so.6 => not found
        	libQt6Core.so.6 => not found
        	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fbfe2c8e000)
        	libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fbfe2aac000)
        	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fbfe295d000)
        	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fbfe2940000)
        	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbfe274e000)
        	libltdl.so.7 => /lib/x86_64-linux-gnu/libltdl.so.7 (0x00007fbfe2743000)
        	/lib64/ld-linux-x86-64.so.2 (0x00007fbfe2d58000)
        	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fbfe273d000)
        

        I guess the correct one is /lib/x86_64-linux-gnu/libodbc.so.2 :

        QString connectionString = QStringLiteral(
            "DRIVER=/lib/x86_64-linux-gnu/libodbc.so.2;"
            "SERVERNODE=192.168.2.22:1433;"
            "UID=USER;"
            "PWD=PASSWORD;");
        

        but the output is the same:

        QSqlError("0", "QODBC: Unable to connect", "[unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed")

        piervalliP artwawA 2 Replies Last reply
        0
        • M Mark81

          @artwaw I read that page but I thought that example was specific for HANA db!
          Anyway I think I have to select the proper library, in my case from:

          $ ldd libqsqlodbc.so 
          	linux-vdso.so.1 (0x00007ffeb81d5000)
          	libodbc.so.2 => /lib/x86_64-linux-gnu/libodbc.so.2 (0x00007fbfe2cb1000)
          	libQt6Sql.so.6 => not found
          	libQt6Core.so.6 => not found
          	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fbfe2c8e000)
          	libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fbfe2aac000)
          	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fbfe295d000)
          	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fbfe2940000)
          	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbfe274e000)
          	libltdl.so.7 => /lib/x86_64-linux-gnu/libltdl.so.7 (0x00007fbfe2743000)
          	/lib64/ld-linux-x86-64.so.2 (0x00007fbfe2d58000)
          	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fbfe273d000)
          

          I guess the correct one is /lib/x86_64-linux-gnu/libodbc.so.2 :

          QString connectionString = QStringLiteral(
              "DRIVER=/lib/x86_64-linux-gnu/libodbc.so.2;"
              "SERVERNODE=192.168.2.22:1433;"
              "UID=USER;"
              "PWD=PASSWORD;");
          

          but the output is the same:

          QSqlError("0", "QODBC: Unable to connect", "[unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed")

          piervalliP Offline
          piervalliP Offline
          piervalli
          wrote on last edited by
          #4

          @Mark81 Usually I use this link to check the connection string. https://www.connectionstrings.com/
          On Windows it works.

          M 1 Reply Last reply
          0
          • M Mark81

            @artwaw I read that page but I thought that example was specific for HANA db!
            Anyway I think I have to select the proper library, in my case from:

            $ ldd libqsqlodbc.so 
            	linux-vdso.so.1 (0x00007ffeb81d5000)
            	libodbc.so.2 => /lib/x86_64-linux-gnu/libodbc.so.2 (0x00007fbfe2cb1000)
            	libQt6Sql.so.6 => not found
            	libQt6Core.so.6 => not found
            	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fbfe2c8e000)
            	libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fbfe2aac000)
            	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fbfe295d000)
            	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fbfe2940000)
            	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fbfe274e000)
            	libltdl.so.7 => /lib/x86_64-linux-gnu/libltdl.so.7 (0x00007fbfe2743000)
            	/lib64/ld-linux-x86-64.so.2 (0x00007fbfe2d58000)
            	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fbfe273d000)
            

            I guess the correct one is /lib/x86_64-linux-gnu/libodbc.so.2 :

            QString connectionString = QStringLiteral(
                "DRIVER=/lib/x86_64-linux-gnu/libodbc.so.2;"
                "SERVERNODE=192.168.2.22:1433;"
                "UID=USER;"
                "PWD=PASSWORD;");
            

            but the output is the same:

            QSqlError("0", "QODBC: Unable to connect", "[unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed")

            artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #5

            @Mark81 said in Connection string for ODBC / Microsoft SQL Express:

            Driver's SQLAllocHandle on SQL_HANDLE_HENV failed

            That's completely different error now and sadly, I suggest googling for it in the context of ODBC in general. First of the results offers different potential solutions https://stackoverflow.com/questions/55474713/drivers-sqlallochandle-on-sql-handle-henv-failed-0-sqldriverconnect-when-co

            For more information please re-read.

            Kind Regards,
            Artur

            M 1 Reply Last reply
            0
            • artwawA artwaw

              @Mark81 said in Connection string for ODBC / Microsoft SQL Express:

              Driver's SQLAllocHandle on SQL_HANDLE_HENV failed

              That's completely different error now and sadly, I suggest googling for it in the context of ODBC in general. First of the results offers different potential solutions https://stackoverflow.com/questions/55474713/drivers-sqlallochandle-on-sql-handle-henv-failed-0-sqldriverconnect-when-co

              M Offline
              M Offline
              Mark81
              wrote on last edited by
              #6

              @artwaw I didn't find this information in the Qt docs pages - at least not in the pages I read.
              So it's beyond my knowledge right now. The solutions proposed are very specific (they talks about Python, docker and Windows). I'll try to guess what I missed..

              1 Reply Last reply
              0
              • piervalliP piervalli

                @Mark81 Usually I use this link to check the connection string. https://www.connectionstrings.com/
                On Windows it works.

                M Offline
                M Offline
                Mark81
                wrote on last edited by
                #7

                @piervalli from that site I grabbed the first connection string I reported in the initial question. But it was wrong... so I'm not sure if it applies to the Qt implementation too.

                artwawA 1 Reply Last reply
                0
                • M Mark81

                  @piervalli from that site I grabbed the first connection string I reported in the initial question. But it was wrong... so I'm not sure if it applies to the Qt implementation too.

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on last edited by
                  #8

                  @Mark81 my understanding of your current situation is that it goes beyond Qt (as in Qt part is done right). Seems to me the problem now lies in ODBC configuration in the system.

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    mchinand
                    wrote on last edited by
                    #9

                    Can you connect to it via another ODBC client (i.e., one not based on Qt)?

                    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