Description:
i want to execute multiple statements in one sql strint, so i add OPTION=67108864 when i connenct mysql server, and in my insert sql string ,i use SQLBindParameter bind 3 rows, but when the sql execute, it only insert 1 row.
How to repeat:
mysql version:5.5.21
code:
#include <sql.h>
#include <sqltypes.h>
#include <sqlext.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct test test_t;
struct test
{
int i;
int j;
SQLINTEGER len1;
SQLINTEGER len2;
};
int main()
{
char* conn_str= "SERVER=127.0.0.1;DRIVER={MySQL ODBC 5.1 Driver};PORT=3306;DATABASE=test;USER=utest;PASSWORD=qwe123;OPTION=67108864";
HENV hevn;
HDBC hdbc;
SQLRETURN rc = SQL_SUCCESS;
char out_conn_str[1024];
SQLSMALLINT out_len;
HSTMT hstmt;
test_t data[3];
int len1[3];
int len2[3];
int i;
SQLCHAR* sql = (SQLCHAR*)"insert into test values(?,?);commit;";
SQLLEN rowcount = 0;
data[0].i = 1;
data[0].j = 1;
data[1].i = 2;
data[1].j = 2;
data[2].i = 3;
data[2].j = 3;
for (i = 0 ; i < 3; i++)
{
data[i].len1 = 4;
data[i].len2 = 4;
}
SQLAllocHandle(SQL_HANDLE_ENV, NULL, &hevn);
SQLSetEnvAttr(hevn, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
SQLAllocHandle(SQL_HANDLE_DBC, hevn, &hdbc);
rc |= SQLDriverConnect(hdbc, NULL, (SQLCHAR*)conn_str, strlen(conn_str), (SQLCHAR*)out_conn_str,
sizeof(out_conn_str), &out_len, SQL_DRIVER_NOPROMPT);
if (rc != SQL_SUCCESS)
{
sql_get_error_info(SQL_HANDLE_DBC, hdbc, rc, __LINE__, __FILE__);
printf("error\n");
return -1;
}
rc |= SQLSetConnectAttr(hdbc, SQL_ATTR_AUTOCOMMIT, (SQLPOINTER)SQL_AUTOCOMMIT_OFF, 0);
if (rc != SQL_SUCCESS)
{
sql_get_error_info(SQL_HANDLE_DBC, hdbc, rc, __LINE__, __FILE__);
printf("error\n");
return -1;
}
SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
rc |= SQLSetStmtAttr(hstmt, SQL_ATTR_PARAM_BIND_TYPE, (SQLPOINTER)sizeof(test_t), 0);
rc |= SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_INTEGER, 0, 0, &data[0].i, 0, &data[0].len1);
rc |= SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_SHORT, SQL_INTEGER, 0, 0, &data[0].j, 0, &data[0].len2);
rc |= SQLSetStmtAttr(hstmt, SQL_ATTR_PARAMSET_SIZE, (SQLPOINTER)3, 0);
rc |= SQLPrepare(hstmt, sql, strlen(sql));
if (rc != SQL_SUCCESS)
{
printf("error\n");
return -1;
}
rc |= SQLExecute(hstmt);
rc |= SQLRowCount(hstmt, &rowcount);
printf("%d\n", rowcount);
SQLFreeStmt(hstmt, SQL_CLOSE);
SQLFreeConnect(hdbc);
SQLFreeEnv(hevn);
return 0;
}