Ознакомление с базовыми функциями выборки данных в ODBC, страница 4

if ((code == SQL_SUCCESS)||(code == SQL_SUCCESS_WITH_INFO))

printf("Connection descriptor is allocated...\n");

else

{

printf("Error when try to set connection descriptor!\n");

End(env);

}

//----------------------------------------------------------------------------/* Connection with data source */

code = SQLConnect(dbc,"MYDB",SQL_NTS,"pm8304",SQL_NTS,"novosibirsk",SQL_NTS);

if (code != SQL_SUCCESS)

{

printf("Error when try to connect to data source!\n");

End(env);

}

printf("Connection with data source is successful...\n");

//----------------------------------------------------------------------------/* Set operator's identifier */

code = SQLAllocStmt (dbc, &stmt);

if ((code == SQL_SUCCESS) || (code == SQL_SUCCESS_WITH_INFO))   

printf("Operator's identifier is allocated...\n");   

else                                                      

{

printf("Error is occured while allocated operator's identifier!\n");

SQLError(env,dbc,stmt,sqlstate, &nativeerr, errmsg,

SQL_MAX_MESSAGE_LENGTH - 1, &actualmsglen);

printf("SQLSTATE = %s\nNATIVE ERROR = %d\n", sqlstate, nativeerr);

errmsg[actualmsglen] = '\0';

printf("MSG = %s\n", errmsg);

End(env);

}                                                              

//----------------------------------------------------------------------------/* Create query */

strcpy((char*)sql,"\0");

strcat((char*)sql,"SELECT n_post,surname,rating,city FROM s WHERE n_post in

(SELECT DISTINCT n_post FROM spj WHERE n_det = ?)");

/* Prepare query */            

code = SQLPrepare(stmt,sql,(SDWORD)strlen((char*)sql));

if (code == SQL_ERROR) { printf("Query is not prepared...\n"); End(env);}

printf("Query is prepared...\n");

/* Set parameters values */

code = SQLBindParameter(stmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,6,0,

n_det,0,&cbTest);

if (code == SQL_ERROR) End(env);

printf("Parameters are defined...\n");

/* Execute query */

code = SQLExecute(stmt);

if ((code == SQL_SUCCESS) || (code == SQL_SUCCESS_WITH_INFO))   

printf("Query has been executed successful...\n");   

else                                                     

{

printf("Error is occured during query execution!\n");

SQLError(env,dbc,stmt,sqlstate, &nativeerr, errmsg,

SQL_MAX_MESSAGE_LENGTH - 1, &actualmsglen);

printf("SQLSTATE = %s\nNATIVE ERROR = %d\n", sqlstate, nativeerr);

errmsg[actualmsglen] = '\0';

printf("MSG = %s\n", errmsg);

End(env);

}

//-----------------------------------------------------------------------------     

/* Choose data */

printf("\nResults:\n");

code = SQLFetch(stmt);

if (code == SQL_ERROR)

{

printf("Error is occured during fetching data!\n");

SQLError(env,dbc,stmt,sqlstate, &nativeerr, errmsg,

SQL_MAX_MESSAGE_LENGTH - 1, &actualmsglen);

printf("SQLSTATE = %s\nNATIVE ERROR = %d\n", sqlstate, nativeerr);

errmsg[actualmsglen] = '\0';

printf("MSG = %s\n", errmsg);

End(env);

}

while (code != SQL_NO_DATA_FOUND)

{

code = SQLGetData(stmt,1,SQL_C_CHAR,n_post,(SDWORD)sizeof(n_post),&val_n_post);

if (code == SQL_ERROR)

{

printf("Error is occured during getting data from the first column!\n");

SQLError(env,dbc,stmt,sqlstate, &nativeerr, errmsg,

SQL_MAX_MESSAGE_LENGTH - 1, &actualmsglen);

printf("SQLSTATE = %s\nNATIVE ERROR = %d\n", sqlstate, nativeerr);

errmsg[actualmsglen] = '\0';

printf("MSG = %s\n", errmsg);

End(env);

}

code = SQLGetData(stmt,2,SQL_C_CHAR,surname,(SDWORD)sizeof(surname),&val_surname);

if (code == SQL_ERROR)

{