C++ DB : SQLServer : SQLExecute - Insert statement returning Error -2
-
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;
} -
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;
}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.
-
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.
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
-
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
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!
-
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!
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
-
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
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
-
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
-
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
-
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