First of all, please forgive my newness to the language. I have had
much experience programming...b ut not in C! :)
I am modifying a program designed in LabWindows/CVI, which as far as I
can tell is a version of ANSI C. The program needs to communicate
with my AS/400 via sql/odbc to retrieve some information about a
barcode scanned part.
This works. Once. After that, it blows up on the SQLPrepare
command. I have no idea why, and I really don't understand where I've
failed. Any guidance would be greatly appreciated as I try to learn
this language in a matter of days to complete this project... >_<
Below you will find the procedures that I have added for the AS400
connection, per research I did online. In the opening process of the
program it calls DBopen. Then, I am simulating a barcode scan by
calling a procedure TestAS400. It asks for a barcode label and then
uses that in a dynamic sql statement. As I said, this works the first
time. The first time I get the following output:
--------------------------
[data]
DONE,BYE
--------------------------
The second time I get the last 15 characters of the sql statement
followed by 'Error or SQLPrepare!!', then "error!', then "DONE,BYE".
I just don't get it! Is my connection not being closed properly? How
do I fix this? Any ideas are very much appreciated.
Oh, and even though I only wanted one field returned from my sql
statement, I had to make the statement ask for two or my variable
would be corrupted?! I really am being tossed in to this. Sorry if
these are newbie errors.
Procedures:
--------------------------
int DBopen(void)
{
int res=0;
RETCODE retcode;
printf("DBopen Start\n");
/*allocate the environment handle*/
if(SQLAllocEnv( &henv)==SQL_SUC CESS)
{
/*allocate the connection handle*/
if(SQLAllocConn ect(henv, &hdbc)==SQL_SUC CESS)
{
/* Set login timeout to 5 seconds. */
//SQLSetConnectOp tion(hdbc, SQL_LOGIN_TIMEO UT, 5);
//SQLSetConnectOp tion(hdbc, SQL_CURSOR_TYPE ,
SQL_CURSOR_STAT IC);
/* Connect to data source */
retcode = SQLConnect(hdbc , "iSeries", SQL_NTS, "USER",
SQL_NTS, "PWD", SQL_NTS);
if (retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WIT H_INFO)
{
res=1;
}
}
else
{
SQLFreeConnect( hdbc);
}
}
else
{
SQLFreeEnv(henv );
}
dbopen=res;
return res;
}
void DBclose(void)
{
if(dbopen)
{
SQLDisconnect(h dbc);
SQLFreeConnect( hdbc);
SQLFreeEnv(henv );
}
dbopen=0;
}
int DBexecute(char *sql,HSTMT *hstmt)
{
int res=0;
RETCODE retcode;
if(SQLAllocStmt (hdbc, hstmt)== SQL_SUCCESS)
{
retcode=SQLPrep are(*hstmt,sql, strlen(sql));
if(retcode==SQL _SUCCESS)
{
retcode=SQLExec ute(*hstmt);
if(retcode==SQL _SUCCESS)
{
res=1;
}
else
{
printf("Error on SQLExecute!!\n" );
}
}
else
{
printf(sql);
printf("Error on SQLPrepare!!\n" );
}
}
else
{
printf("Error on SQLAllocStmt!!\ n");
}
return res;
}
void DBcloseCursor(H STMT hstmt)
{
SQLFreeStmt(hst mt, SQL_DROP);
}
void TestAS400()
{
char sql[160];
char *sql2;
HSTMT fstmt;
long lens;
RETCODE retcode;
char name[2 + 1];
char szBarcode[13 + 1];
scanf("%s", szBarcode );
sql2 = strcat("SELECT field1,field2 FROM library.file where field3 =
(select field4 from library.file where field5 ='",szBarcode );
sql2 = strcat(sql2,"') \n");
if(DBexecute(sq l2,&fstmt))
{
SQLBindCol(fstm t,1,SQL_C_CHAR, name,sizeof(nam e),&lens);
retcode = SQLFetch(fstmt) ;
while(retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WIT H_INFO)
{
printf("%s\n",n ame);
retcode = SQLFetch(fstmt) ;
}
DBcloseCursor(f stmt);
}
else
{
printf("error!\ n");
}
printf("DONE,BY E\n");
//DBclose();
}
much experience programming...b ut not in C! :)
I am modifying a program designed in LabWindows/CVI, which as far as I
can tell is a version of ANSI C. The program needs to communicate
with my AS/400 via sql/odbc to retrieve some information about a
barcode scanned part.
This works. Once. After that, it blows up on the SQLPrepare
command. I have no idea why, and I really don't understand where I've
failed. Any guidance would be greatly appreciated as I try to learn
this language in a matter of days to complete this project... >_<
Below you will find the procedures that I have added for the AS400
connection, per research I did online. In the opening process of the
program it calls DBopen. Then, I am simulating a barcode scan by
calling a procedure TestAS400. It asks for a barcode label and then
uses that in a dynamic sql statement. As I said, this works the first
time. The first time I get the following output:
--------------------------
[data]
DONE,BYE
--------------------------
The second time I get the last 15 characters of the sql statement
followed by 'Error or SQLPrepare!!', then "error!', then "DONE,BYE".
I just don't get it! Is my connection not being closed properly? How
do I fix this? Any ideas are very much appreciated.
Oh, and even though I only wanted one field returned from my sql
statement, I had to make the statement ask for two or my variable
would be corrupted?! I really am being tossed in to this. Sorry if
these are newbie errors.
Procedures:
--------------------------
int DBopen(void)
{
int res=0;
RETCODE retcode;
printf("DBopen Start\n");
/*allocate the environment handle*/
if(SQLAllocEnv( &henv)==SQL_SUC CESS)
{
/*allocate the connection handle*/
if(SQLAllocConn ect(henv, &hdbc)==SQL_SUC CESS)
{
/* Set login timeout to 5 seconds. */
//SQLSetConnectOp tion(hdbc, SQL_LOGIN_TIMEO UT, 5);
//SQLSetConnectOp tion(hdbc, SQL_CURSOR_TYPE ,
SQL_CURSOR_STAT IC);
/* Connect to data source */
retcode = SQLConnect(hdbc , "iSeries", SQL_NTS, "USER",
SQL_NTS, "PWD", SQL_NTS);
if (retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WIT H_INFO)
{
res=1;
}
}
else
{
SQLFreeConnect( hdbc);
}
}
else
{
SQLFreeEnv(henv );
}
dbopen=res;
return res;
}
void DBclose(void)
{
if(dbopen)
{
SQLDisconnect(h dbc);
SQLFreeConnect( hdbc);
SQLFreeEnv(henv );
}
dbopen=0;
}
int DBexecute(char *sql,HSTMT *hstmt)
{
int res=0;
RETCODE retcode;
if(SQLAllocStmt (hdbc, hstmt)== SQL_SUCCESS)
{
retcode=SQLPrep are(*hstmt,sql, strlen(sql));
if(retcode==SQL _SUCCESS)
{
retcode=SQLExec ute(*hstmt);
if(retcode==SQL _SUCCESS)
{
res=1;
}
else
{
printf("Error on SQLExecute!!\n" );
}
}
else
{
printf(sql);
printf("Error on SQLPrepare!!\n" );
}
}
else
{
printf("Error on SQLAllocStmt!!\ n");
}
return res;
}
void DBcloseCursor(H STMT hstmt)
{
SQLFreeStmt(hst mt, SQL_DROP);
}
void TestAS400()
{
char sql[160];
char *sql2;
HSTMT fstmt;
long lens;
RETCODE retcode;
char name[2 + 1];
char szBarcode[13 + 1];
scanf("%s", szBarcode );
sql2 = strcat("SELECT field1,field2 FROM library.file where field3 =
(select field4 from library.file where field5 ='",szBarcode );
sql2 = strcat(sql2,"') \n");
if(DBexecute(sq l2,&fstmt))
{
SQLBindCol(fstm t,1,SQL_C_CHAR, name,sizeof(nam e),&lens);
retcode = SQLFetch(fstmt) ;
while(retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WIT H_INFO)
{
printf("%s\n",n ame);
retcode = SQLFetch(fstmt) ;
}
DBcloseCursor(f stmt);
}
else
{
printf("error!\ n");
}
printf("DONE,BY E\n");
//DBclose();
}
Comment