#ifdef _WIN32 #include #endif #include #include #include SQLHENV henv; SQLHDBC hdbc, hdbc2; SQLHSTMT hstmt, hstmt2; #define ok_something(A, B, C) rc= B; if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO){ \ error_num= PrintError(C, A, rc); \ goto PROGRAM_END; \ } #define ok_env(A, B) ok_something(A,B, SQL_HANDLE_ENV) #define ok_dbc(A, B) ok_something(A,B, SQL_HANDLE_DBC) #define ok_stmt(A, B) ok_something(A,B, SQL_HANDLE_STMT) SQLINTEGER PrintError(SQLSMALLINT handleType, SQLHANDLE handle, SQLRETURN rcode) { SQLRETURN rc = SQL_ERROR; SQLCHAR sqlState[6]; SQLCHAR eMsg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER nError; SQLSMALLINT msgLen; if (rcode == SQL_NO_DATA) { printf( "NO DATA .. .. .."); return(SQL_NO_DATA); } else { rc = SQLGetDiagRec(handleType, handle, 1, sqlState, &nError, eMsg, sizeof(eMsg), &msgLen); if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) { printf( " Error: %s\n", eMsg); } return (SQL_ERROR); } } #define CONN_STRING "Driver={MySQL ODBC 5.1 Driver};Server=localhost;User=root;Password=braindead;Database=test;MULTI_STATEMENTS=1" void testInsert(SQLRETURN rc,int testNumber, char *message1, char*message2) { int nRow; // if ((rc!=SQL_SUCCESS)&&(rc!=SQL_SUCCESS_WITH_INFO)) SQLRowCount(hstmt,(SQLINTEGER*)&nRow); if (nRow) printf("%s: Insert %i is ok.\n",message1,testNumber); else printf("%s: Insert %i failed.\n",message2,testNumber); } int main(int argc, char * argv[]) { SQLRETURN rc; int error_num=0; ok_env(henv, SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv)); ok_env(henv, SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_NTS)); ok_env(henv, SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc)); ok_dbc(hdbc, SQLDriverConnect(hdbc, NULL, (SQLCHAR*)CONN_STRING, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT)); ok_dbc(hdbc, SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt)); ok_stmt(hstmt, SQLExecDirect(hstmt, (SQLCHAR*)"DROP TABLE IF EXISTS bug53684", SQL_NTS)); ok_stmt(hstmt, SQLExecDirect(hstmt, (SQLCHAR*)"CREATE TABLE bug53684(id INT NOT NULL PRIMARY KEY, name VARCHAR(32));", SQL_NTS)); ok_stmt(hstmt, SQLExecDirect(hstmt, (SQLCHAR*)"INSERT INTO bug53684 (id, name) VALUES (1, 'Frank'), (2, 'Sabrina'), (3, 'David')", SQL_NTS)); char *sqlQuery="INSERT INTO bug53684 (id, name) VALUES(4, 'Dominique');" "INSERT INTO bug53684 (id, name) VALUES(1, 'Sienna');" "INSERT INTO bug53684 (id, name) VALUES(5, 'Youness');"; ok_stmt(hstmt, SQLPrepare(hstmt, (SQLCHAR*)sqlQuery, (SQLINTEGER)strlen(sqlQuery))); // this rc is related to the "first insert": rc= SQLExecute(hstmt); testInsert(rc,1,"OK","ERROR"); // this rc is related to the "second insert": rc= SQLMoreResults(hstmt); testInsert(rc,2,"ERROR","OK"); // this rc is related to the "third insert": rc= SQLMoreResults(hstmt); testInsert(rc,3,"OK","ERROR"); { sqlQuery="Select count(id) from bug53684;"; char nRowTotal[10]; ok_stmt(hstmt, SQLPrepare(hstmt,(SQLCHAR*)sqlQuery, (SQLINTEGER)strlen(sqlQuery))); ok_stmt(hstmt, SQLBindCol(hstmt,1,SQL_C_CHAR,&nRowTotal,10,NULL)); ok_stmt(hstmt, SQLExecute(hstmt)); ok_stmt(hstmt, SQLFetchScroll (hstmt,SQL_FETCH_NEXT,0)); if (strcmp(nRowTotal,"5")) printf("ERROR: total number of rows in table should be 5 (currently:%s)\n",nRowTotal); } PROGRAM_END: // ok_stmt(hstmt, SQLExecDirect(hstmt, (SQLCHAR*)"DROP TABLE IF EXISTS bug53684", SQL_NTS)); rc = SQLDisconnect(hdbc); rc = SQLFreeConnect(hdbc); rc = SQLFreeEnv(henv); }