Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ DB : SQLServer : SQLExecute - Insert statement returning Error -2

C++ DB : SQLServer : SQLExecute - Insert statement returning Error -2

Scheduled Pinned Locked Moved C / C++ / MFC
databasec++helpjson
9 Posts 3 Posters 0 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.
  • S Offline
    S Offline
    SureshBL
    wrote on last edited by
    #1

    Hi C++ Gurus,

    Below code is generating error code -2. Its working fine for sql queries like "select * from emp", but where as in Insert statement its returning error code. Please help.

    Regards,
    Suresh

    #include "database.h"

    int main(){

    SQLRETURN ret = 0; /* ODBC API return status */

    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;

    SQLSMALLINT msg_len = 0;
    SQLCHAR sql_state[6], message[256];
    SQLINTEGER native_error = 0;

    try{
    ret = SQLAllocEnv(&env);
    ret = SQLAllocConnect(env, &dbc);
    ret = SQLConnect(dbc, (SQLCHAR*)"KarTarDB", strlen("KarTarDB"), (SQLCHAR*)"sa",2,(SQLCHAR*)"KarTarPwd",9);

    if(ret == SQL\_INVALID\_HANDLE || ret < 0) {
        cout << "Connection open failure." << endl;
        ret = SQLGetDiagRec(SQL\_HANDLE\_STMT, stmt, 1, sql\_state, &native\_error, message, sizeof(message), &msg\_len);
        cout << ret << endl;
    } else{
        char sql\[100\]="INSERT INTO emp(name, age, dob) VALUES ('john', 23, '010101')";
    
        ret = SQLPrepare(stmt,(unsigned char\*)sql, SQL\_NTS);//strlen(sql));
        ret = SQLExecute(stmt);//,(SQLCHAR\*)sql,strlen(sql));
    
        if(ret == SQL\_ERROR || ret < 0) {
            cout << "ResultSet Error: " << ret << endl;
            //ret = SQLGetDiagRec(SQL\_HANDLE\_STMT, stmt, 1, sql\_state, &native\_error, message, sizeof(message), &msg\_len);
        }else{
            cout << "Done" << endl;
        }
    }
    

    }catch(...) {
    cout << "Database error.." << endl;
    }
    return 0;
    }

    V 1 Reply Last reply
    0
    • S SureshBL

      Hi C++ Gurus,

      Below code is generating error code -2. Its working fine for sql queries like "select * from emp", but where as in Insert statement its returning error code. Please help.

      Regards,
      Suresh

      #include "database.h"

      int main(){

      SQLRETURN ret = 0; /* ODBC API return status */

      SQLHENV env;
      SQLHDBC dbc;
      SQLHSTMT stmt;

      SQLSMALLINT msg_len = 0;
      SQLCHAR sql_state[6], message[256];
      SQLINTEGER native_error = 0;

      try{
      ret = SQLAllocEnv(&env);
      ret = SQLAllocConnect(env, &dbc);
      ret = SQLConnect(dbc, (SQLCHAR*)"KarTarDB", strlen("KarTarDB"), (SQLCHAR*)"sa",2,(SQLCHAR*)"KarTarPwd",9);

      if(ret == SQL\_INVALID\_HANDLE || ret < 0) {
          cout << "Connection open failure." << endl;
          ret = SQLGetDiagRec(SQL\_HANDLE\_STMT, stmt, 1, sql\_state, &native\_error, message, sizeof(message), &msg\_len);
          cout << ret << endl;
      } else{
          char sql\[100\]="INSERT INTO emp(name, age, dob) VALUES ('john', 23, '010101')";
      
          ret = SQLPrepare(stmt,(unsigned char\*)sql, SQL\_NTS);//strlen(sql));
          ret = SQLExecute(stmt);//,(SQLCHAR\*)sql,strlen(sql));
      
          if(ret == SQL\_ERROR || ret < 0) {
              cout << "ResultSet Error: " << ret << endl;
              //ret = SQLGetDiagRec(SQL\_HANDLE\_STMT, stmt, 1, sql\_state, &native\_error, message, sizeof(message), &msg\_len);
          }else{
              cout << "Done" << endl;
          }
      }
      

      }catch(...) {
      cout << "Database error.." << endl;
      }
      return 0;
      }

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      What is -2? According to MSDN [SQLExecute Function - SQL Server | Microsoft Docs](https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlexecute-function?view=sql-server-ver15) it can return the following:

      Quote:

      SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_NEED_DATA, SQL_STILL_EXECUTING, SQL_ERROR, SQL_NO_DATA, SQL_INVALID_HANDLE, or SQL_PARAM_DATA_AVAILABLE.

      Besides

      Quote:

      When SQLExecute returns either SQL_ERROR or SQL_SUCCESS_WITH_INFO, an associated SQLSTATE value can be obtained by calling SQLGetDiagRec with a HandleType of SQL_HANDLE_STMT and a Handle of StatementHandle.

      S 1 Reply Last reply
      0
      • V Victor Nijegorodov

        What is -2? According to MSDN [SQLExecute Function - SQL Server | Microsoft Docs](https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlexecute-function?view=sql-server-ver15) it can return the following:

        Quote:

        SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_NEED_DATA, SQL_STILL_EXECUTING, SQL_ERROR, SQL_NO_DATA, SQL_INVALID_HANDLE, or SQL_PARAM_DATA_AVAILABLE.

        Besides

        Quote:

        When SQLExecute returns either SQL_ERROR or SQL_SUCCESS_WITH_INFO, an associated SQLSTATE value can be obtained by calling SQLGetDiagRec with a HandleType of SQL_HANDLE_STMT and a Handle of StatementHandle.

        S Offline
        S Offline
        SureshBL
        wrote on last edited by
        #3

        Hi Victor, Thanks for your response. As you can see in the code, I have already used SQLGetDiagRec.

        ret = SQLGetDiagRec(SQL_HANDLE_STMT, stmt, 1, sql_state, &native_error, message, sizeof(message), &msg_len)

        But even that is returning -2 and not giving any other details, hence I commented it. Am I missing anything? How can I get more details from ret object apart from -2. Regards, Suresh

        V 1 Reply Last reply
        0
        • S SureshBL

          Hi Victor, Thanks for your response. As you can see in the code, I have already used SQLGetDiagRec.

          ret = SQLGetDiagRec(SQL_HANDLE_STMT, stmt, 1, sql_state, &native_error, message, sizeof(message), &msg_len)

          But even that is returning -2 and not giving any other details, hence I commented it. Am I missing anything? How can I get more details from ret object apart from -2. Regards, Suresh

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          SureshBL wrote:

          But even that is returning -2 and not giving any other details, hence I commented it. Am I missing anything? How can I get more details from ret object apart from -2.

          Well, -2 means SQL_INVALID_HANDLE. So you need to go one step back and check why the handle is invalid!

          S 1 Reply Last reply
          0
          • V Victor Nijegorodov

            SureshBL wrote:

            But even that is returning -2 and not giving any other details, hence I commented it. Am I missing anything? How can I get more details from ret object apart from -2.

            Well, -2 means SQL_INVALID_HANDLE. So you need to go one step back and check why the handle is invalid!

            S Offline
            S Offline
            SureshBL
            wrote on last edited by
            #5

            Thanks Do you see anything wrong with my Code? As mentioned querying table is working fine with the connection, but Insert statement is making handle invalid. SQLSTATE: l NativeError: 0 ErrMsg: 0x6cfce0 Apologies, I am learning c++ now. So I am not expert to understand lots of bits and pieces to debug the issue in every step. Regards, Suresh

            J 1 Reply Last reply
            0
            • S SureshBL

              Thanks Do you see anything wrong with my Code? As mentioned querying table is working fine with the connection, but Insert statement is making handle invalid. SQLSTATE: l NativeError: 0 ErrMsg: 0x6cfce0 Apologies, I am learning c++ now. So I am not expert to understand lots of bits and pieces to debug the issue in every step. Regards, Suresh

              J Online
              J Online
              jeron1
              wrote on last edited by
              #6

              Can't say I've ever used this, but shouldn't the SQLHSTMT variable 'stmt' be initialized with something?

              "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

              S 2 Replies Last reply
              0
              • J jeron1

                Can't say I've ever used this, but shouldn't the SQLHSTMT variable 'stmt' be initialized with something?

                "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                S Offline
                S Offline
                SureshBL
                wrote on last edited by
                #7

                Ok got you. So I have to assign dbc to stmt? ret = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);

                1 Reply Last reply
                0
                • J jeron1

                  Can't say I've ever used this, but shouldn't the SQLHSTMT variable 'stmt' be initialized with something?

                  "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                  S Offline
                  S Offline
                  SureshBL
                  wrote on last edited by
                  #8

                  Great Its working now. Thank you very much.. :)

                  J 1 Reply Last reply
                  0
                  • S SureshBL

                    Great Its working now. Thank you very much.. :)

                    J Online
                    J Online
                    jeron1
                    wrote on last edited by
                    #9

                    You're welcome. :)

                    "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups