#include "mytest3.h" SQLRETURN rc; void my_tstidx(SQLHDBC hdbc, SQLHSTMT hstmt) { SQLCHAR szChp1[16] = {0}; SQLINTEGER nChp2 = 500, nlen, row_nb; SQLCHAR szChp3[16] = {0}; myheader("tstidx"); tmysql_exec(hstmt,"drop table tstidx"); rc = tmysql_exec(hstmt,"create table tstidx(chp1 char(3) default null, chp2 int default null, chp3 char(10) default null)"); mystmt(hstmt,rc); rc = tmysql_exec(hstmt,"create unique index tstidx1 on tstidx (chp1)"); mystmt(hstmt,rc); rc = tmysql_exec(hstmt,"create unique index tstidx2 on tstidx (chp3, chp1)"); mystmt(hstmt,rc); rc = tmysql_exec(hstmt,"create index tstidx3 on tstidx (chp2)"); mystmt(hstmt,rc); rc = tmysql_exec(hstmt,"insert into tstidx values('AAA', 100,'MySQL1')"); mystmt(hstmt,rc); rc = tmysql_exec(hstmt,"insert into tstidx values('BBB', 200,'MySQL2')"); mystmt(hstmt,rc); rc = tmysql_exec(hstmt,"insert into tstidx values('CCC', 300,'MySQL3')"); mystmt(hstmt,rc); rc = SQLTransact(NULL,hdbc,SQL_COMMIT); mycon(hdbc,rc); rc = SQLFreeStmt(hstmt,SQL_CLOSE); mystmt(hstmt,rc); rc = SQLPrepare(hstmt, (SQLCHAR *) "select * from tstidx", SQL_NTS); mystmt(hstmt,rc); rc = SQLBindCol(hstmt,1,SQL_C_CHAR,szChp1,16,NULL); mystmt(hstmt,rc); rc = SQLBindCol(hstmt,2,SQL_C_LONG,&nChp2,100,NULL); mystmt(hstmt,rc); rc = SQLBindCol(hstmt,3,SQL_C_CHAR,szChp3,16,NULL); mystmt(hstmt,rc); rc = SQLExecute(hstmt); mystmt(hstmt,rc); row_nb = 0; do { rc = SQLFetchScroll(hstmt, SQL_FETCH_NEXT, 1); if (rc == SQL_SUCCESS) { row_nb++; myprintf(" row %d: %s,%d,%s\n",row_nb,szChp1,nChp2,szChp3); } } while (rc == SQL_SUCCESS); rc = SQLFreeStmt(hstmt,SQL_CLOSE); mystmt(hstmt,rc); rc = SQLPrepare(hstmt, (SQLCHAR *) "select * from tstidx where chp1=?", SQL_NTS); mystmt(hstmt,rc); rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, szChp1, 11, NULL); strcpy(szChp1, "BBB"); rc = SQLExecute(hstmt); mystmt(hstmt,rc); rc = SQLFetchScroll(hstmt, SQL_FETCH_NEXT, 1); mystmt(hstmt,rc); nChp2 = 1000; rc = SQLSetPos(hstmt, 1, SQL_UPDATE, SQL_LOCK_NO_CHANGE); mystmt(hstmt,rc); rc = SQLRowCount(hstmt,&nlen); mystmt(hstmt,rc); myprintf(" rows affected:%d\n",nlen); rc = SQLFreeStmt(hstmt,SQL_UNBIND); mystmt(hstmt,rc); rc = SQLFreeStmt(hstmt,SQL_CLOSE); mystmt(hstmt,rc); rc = tmysql_exec(hstmt,"select * from tstidx"); mystmt(hstmt,rc); myresult(hstmt); rc = SQLFreeStmt(hstmt,SQL_CLOSE); mystmt(hstmt,rc); } int main(int argc, char *argv[]) { SQLHENV henv; SQLHDBC hdbc; SQLHSTMT hstmt; SQLCHAR the_dsn[64]; if (argc != 2 || ( argc == 2 && ((!strcmp (argv[1], "-?") || !strcmp (argv[1], "--?") || !strcmp (argv[1], "--h") || !strcmp (argv[1], "--help") )))) { myprintf("------------------------------------------\n"); myprintf("usage: mytest2 [DSN] \n\n"); myprintf(" DSN <-- data source name\n"); exit(0); } mydsn = argv[1]; rc = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv); myenv(henv,rc); rc = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0); myenv(henv,rc); rc = SQLAllocHandle(SQL_HANDLE_DBC,henv, &hdbc); myenv(henv,rc); fprintf(stdout," Connecting to '%s'...\n",mydsn); sprintf(the_dsn, "DSN=%s",mydsn); rc = SQLDriverConnect(hdbc, NULL, (SQLCHAR *) the_dsn, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE); mycon(hdbc,rc); rc = SQLSetConnectAttr(hdbc,SQL_ATTR_AUTOCOMMIT,(SQLPOINTER)SQL_AUTOCOMMIT_ON,0); mycon(hdbc,rc); rc = SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); SQLFreeStmt(hstmt, SQL_CLOSE); SQLSetStmtAttr(hstmt,SQL_ATTR_CURSOR_TYPE,(SQLPOINTER)SQL_CURSOR_STATIC,0); my_tstidx(hdbc, hstmt); mydisconnect(&henv,&hdbc,&hstmt); myprintf("\n\n-- test-end --\n"); return(0); }