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. [Solved] query exec fails when trying to write from qt to MS Sql database in windows using ODBC
Forum Updated to NodeBB v4.3 + New Features

[Solved] query exec fails when trying to write from qt to MS Sql database in windows using ODBC

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.9k 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.
  • A Offline
    A Offline
    Abin
    wrote on last edited by
    #1

    Hi,

    I connect to my MSSQL database with odbc and writing data to it. I am not sure why I get the error:

    QODBCResult::exec: unable to bind variable: "[Microsoft][ODBC SQL Server Driver]Optional feature not implemented"

    bool DataManager::registerClient(Client&)

    "INSERT INTO ClientMaster (first_name, mid_name, last_name, phone_number, email_id, address_line1, address_line2, address_line3, dob) VALUES (xxx, xxx , xxx, +xxx-xxx, xx, xx, x, x, 1987-08-30)"

    bool DataManager::registerClient(Client&) error "[Microsoft][ODBC SQL Server Driver]Optional feature not implemented QODBC3: Unable to bind variable"

    @ mDatabase = QSqlDatabase::addDatabase("QODBC");

    mDatabase.setDatabaseName("DRIVER={SQL SERVER};SERVER=localhost;DATABASE=GLINK_CLIENT_MGMNT;");
    mDatabase.setUserName("sa");
    mDatabase.setPassword("123456");
    
    if(!mDatabase.open())@
    

    @QSqlQuery query(mDatabase);

            query.prepare("INSERT INTO ClientMaster (first_name, mid_name, last_name, phone_number, "
                            "email_id, address_line1, address_line2, address_line3, dob)"
                          "VALUES (:first_name, :mid_name, :last_name, :phone_number, :email_id, "
                            ":address_line1, :address_line2, :address_line3, :dob)");
    
            QString phoneNumber = "+";
            phoneNumber.append(client.phoneCode);
            phoneNumber.append("-");
            phoneNumber.append(client.phoneNumber);
    
            query.bindValue(":first_name", client.firstName);
            query.bindValue(":mid_name", client.midName);
            query.bindValue(":last_name", client.lastName);
            query.bindValue(":phone_number", phoneNumber);
            query.bindValue(":email_id", client.emailId);
            query.bindValue(":address_line1", client.addressLine1);
            query.bindValue(":address_line2", client.addressLine2);
            query.bindValue(":address_line3", client.addressLine3);
            query.bindValue(":dob", client.dob);
    
            regStatus = query.exec();@
    
    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi, just a guess but in your query.prepare() string, line 3: "....address_line3, dob)"
      and line 4: "VALUES (:first_name,..."
      shouldn't it there be a space between them,
      i.e. line 4: " VALUES (:first_name,..."

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Abin
        wrote on last edited by
        #3

        Thanks hskoglund,

        But it doesn't work.. I had found the same and had edited the code. In the logs I show the query executed. You can see there is space before "VALUES". I think the code i posted is before edit. sorry.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clochydd
          wrote on last edited by
          #4

          Hi, did you look at query.lastError after query.exec()?
          And is "xxx, xxx,... " what you expect as the content of client.firstName... ?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Abin
            wrote on last edited by
            #5

            Hi Clochydd,

            lastError is

            “[Microsoft][ODBC SQL Server Driver]Optional feature not implemented QODBC3: Unable to bind variable”

            yes.. there is real data in xxx which comes from client instance. I just hided it with xxx.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              clochydd
              wrote on last edited by
              #6

              Hi Abin, I would try this:

              1. Use only one parameter, if successful two parm...
              2. If this is not useful, replace client.firstName with a QString...
                (I guess the error message points to a problem with datatypes)
              1 Reply Last reply
              0
              • A Offline
                A Offline
                Abin
                wrote on last edited by
                #7

                Thanks all,

                The issue got resolved. I hope the below case is the specific reason for the issue.

                I gave date input in the format (yyyy-MM-dd), ie
                query.bindValue(":dob", client.dob.toString(yyyy-MM-dd));

                It works fine... :)

                I came to know that MS Sql takes date in the particular format only. I not certain about this, however it works fine now...

                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