descW.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /***************************************************************************
  2. * Licensed Materials - Property of IBM and/or HCL
  3. *
  4. * IBM Informix Client-SDK
  5. *
  6. * (C) Copyright IBM Corporation 1997, 2004 All rights reserved.
  7. * (c) Copyright HCL Technologies Ltd. 2017. All Rights Reserved.
  8. *
  9. *
  10. *
  11. *
  12. *
  13. * Title: descW.c (Unicode counterpart of desc.c)
  14. *
  15. * Description: To allocate a single descriptor and use it as the ARD
  16. * for a SELECT statement and as the APD for an INSERT
  17. * statement.
  18. * The data will be selected from the 'customer' table
  19. * and inserted into a new table called 'new_cust'
  20. *
  21. ***************************************************************************
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #ifndef NO_WIN32
  27. #include <io.h>
  28. #include <windows.h>
  29. #include <conio.h>
  30. #endif /*NO_WIN32*/
  31. #include "infxcli.h"
  32. #define BUFFER_LEN 10
  33. #define BUFFER_LENW BUFFER_LEN * sizeof(SQLWCHAR)
  34. #define ERRMSG_LEN 200
  35. SQLWCHAR defDsnW[] = L"odbc_demo";
  36. SQLCHAR defDsn[] = "odbc_demo";
  37. SQLINTEGER checkError (SQLRETURN rc,
  38. SQLSMALLINT handleType,
  39. SQLHANDLE handle,
  40. SQLCHAR* errmsg)
  41. {
  42. SQLRETURN retcode = SQL_SUCCESS;
  43. SQLSMALLINT errNum = 1;
  44. SQLWCHAR sqlStateW[6];
  45. SQLCHAR *sqlState;
  46. SQLINTEGER nativeError;
  47. SQLWCHAR errMsgW[ERRMSG_LEN];
  48. SQLCHAR *errMsg;
  49. SQLSMALLINT textLengthPtr;
  50. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  51. {
  52. while (retcode != SQL_NO_DATA)
  53. {
  54. retcode = SQLGetDiagRecW (handleType, handle, errNum, sqlStateW, &nativeError, errMsgW, ERRMSG_LEN, &textLengthPtr);
  55. if (retcode == SQL_INVALID_HANDLE)
  56. {
  57. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  58. return 1;
  59. }
  60. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  61. {
  62. sqlState = (SQLCHAR *) malloc (wcslen(sqlStateW) + sizeof(char))
  63. ;
  64. wcstombs( (char *) sqlState, sqlStateW, wcslen(sqlStateW)
  65. + sizeof(char));
  66. errMsg = (SQLCHAR *) malloc (wcslen(errMsgW) + sizeof(char));
  67. wcstombs( (char *) errMsg, errMsgW, wcslen(errMsgW)
  68. + sizeof(char));
  69. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState , errMsg);
  70. }
  71. errNum++;
  72. }
  73. fprintf (stderr, "%s\n", errmsg);
  74. return 1; /* all errors on this handle have been reported */
  75. }
  76. else
  77. return 0; /* no errors to report */
  78. }
  79. int main (long argc,
  80. char* argv[])
  81. {
  82. /* Declare variables
  83. */
  84. /* Handles */
  85. SQLHDBC hdbc;
  86. SQLHENV henv;
  87. SQLHSTMT hTableStmt;
  88. SQLHSTMT hSelectStmt;
  89. SQLHSTMT hInsertStmt;
  90. SQLHDESC hdesc;
  91. /* Miscellaneous variables */
  92. SQLWCHAR *dsnW; /*name of the DSN used for connecting to the database*/
  93. SQLRETURN rc = 0;
  94. SQLINTEGER i, in;
  95. SQLWCHAR* createTableStmtW = (SQLWCHAR *) L"CREATE TABLE new_cust( \
  96. cust_num INTEGER,\
  97. fname VARCHAR(10),\
  98. lname VARCHAR(10))";
  99. SQLWCHAR* selectStmtW = (SQLWCHAR *) L"SELECT cust_num, fname, lname \
  100. from customer where cust_num < 110";
  101. SQLWCHAR* insertStmtW = (SQLWCHAR *) L"INSERT INTO new_cust (cust_num, \
  102. fname, lname) VALUES (?, ?, ?)";
  103. SQLINTEGER custnumArray[9]; /*array to hold values of 'cust_num' from table 'customer'*/
  104. SQLCHAR *fnameArray[9];
  105. SQLWCHAR fnameArrayW[9][BUFFER_LEN]; /*array to hold values of 'fname' from table 'customer'*/
  106. SQLCHAR *lnameArray[9];
  107. SQLWCHAR lnameArrayW[9][BUFFER_LEN]; /*array to hold values of 'lname' from table 'customer'*/
  108. SQLINTEGER newCustnum; /*value of 'cust_num' from table 'new_cust'*/
  109. SQLCHAR *newFname;
  110. SQLWCHAR newFnameW[BUFFER_LEN]; /*value of 'fname' from table 'new_cust'*/
  111. SQLCHAR *newLname;
  112. SQLWCHAR newLnameW[BUFFER_LEN]; /*value of 'lname' from table 'new_cust'*/
  113. SQLLEN cbCustnum = 0, cbFname = SQL_NTS, cbLname = SQL_NTS;
  114. int lenArgv1;
  115. /* STEP 1. Get data source name from command line (or use default)
  116. ** Allocate the environment handle and set ODBC version
  117. ** Allocate the connection handle
  118. ** Establish the database connection
  119. ** Allocate the statement handles
  120. ** Allocate the descriptor handle
  121. */
  122. /* If(dsn is not explicitly passed in as arg) */
  123. if (argc != 2)
  124. {
  125. /* Use default dsnW - odbc_demo */
  126. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  127. dsnW = (SQLWCHAR *) malloc( wcslen(defDsnW) * sizeof(SQLWCHAR)
  128. + sizeof(SQLWCHAR) );
  129. wcscpy ((SQLWCHAR *)dsnW, (SQLWCHAR *)defDsnW);
  130. }
  131. else
  132. {
  133. /* Use specified dsnW */
  134. lenArgv1 = strlen((char *)argv[1]);
  135. dsnW = (SQLWCHAR *) malloc (lenArgv1 * sizeof(SQLWCHAR)
  136. + sizeof(SQLWCHAR));
  137. mbstowcs (dsnW, (char *)argv[1], lenArgv1 + sizeof(char));
  138. fprintf (stdout, "\nUsing specified DSN : %s\n", argv[1]);
  139. }
  140. /* Allocate the Environment handle */
  141. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  142. if (rc != SQL_SUCCESS)
  143. {
  144. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  145. return (1);
  146. }
  147. /* Set the ODBC version to 3.0 */
  148. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  149. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  150. return (1);
  151. /* Allocate the connection handle */
  152. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  153. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  154. return (1);
  155. /* Establish the database connection */
  156. rc = SQLConnectW (hdbc, dsnW, SQL_NTS, (SQLWCHAR *) L"", SQL_NTS, (SQLWCHAR *) L"", SQL_NTS);
  157. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
  158. return (1);
  159. /* Allocate the statement handle for the CREATE TABLE statement */
  160. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hTableStmt);
  161. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Create Table Statement Handle Allocation failed\nExiting!!"))
  162. return (1);
  163. /* Allocate the statement handle for the SELECT statement */
  164. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hSelectStmt);
  165. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Select Statement Handle Allocation failed\nExiting!!"))
  166. return (1);
  167. /* Allocate the statement handle for the INSERT statement */
  168. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hInsertStmt);
  169. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Insert Statement Handle Allocation failed\nExiting!!"))
  170. return (1);
  171. /* Allocate the descriptor handle */
  172. rc = SQLAllocHandle (SQL_HANDLE_DESC, hdbc, &hdesc);
  173. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Insert Statement Handle Allocation failed\nExiting!!"))
  174. return (1);
  175. fprintf (stdout, "STEP 1 done...connected to database\n");
  176. /* STEP 2. Create the database table 'new_cust' where the data is to be inserted
  177. */
  178. /* Execute the SQL statement to create the table 'new_cust' */
  179. rc = SQLExecDirectW (hTableStmt, createTableStmtW, SQL_NTS);
  180. if (checkError (rc, SQL_HANDLE_STMT, hTableStmt, (SQLCHAR *) "Error in Step 2 -- SQLExecDirect failed\n" ))
  181. goto Exit;
  182. fprintf (stdout, "STEP 2 done...table new_cust created in the datbase\n");
  183. /* STEP 3. For each column, set the following descriptor fields to appropriate values
  184. ** -- column 'cust_num' : descriptor fields - SQL_DESC_TYPE
  185. ** -- column 'fname' : descriptor fields - SQL_DESC_TYPE, SQL_DESC_LENGTH,
  186. ** SQL_DESC_OCTET_LENGTH
  187. ** -- column 'lname' : descriptor fields - SQL_DESC_TYPE, SQL_DESC_LENGTH,
  188. ** SQL_DESC_OCTET_LENGTH
  189. */
  190. /* Setting descriptor fields for column - cust_num */
  191. rc = SQLSetDescField (hdesc, 1, SQL_DESC_TYPE, (SQLPOINTER) SQL_C_SLONG, 0);
  192. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 3 -- SQLSetDescField (SQL_DESC_DATA_TYPE) failed for column cust_num\n"))
  193. goto Exit;
  194. /* Setting descriptor fields for column - fname */
  195. rc = SQLSetDescFieldW (hdesc, 2, SQL_DESC_TYPE, (SQLPOINTER) SQL_C_WCHAR, 0);
  196. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 3 -- SQLSetDescField (SQL_DESC_DATA_TYPE) failed for column fname\n"))
  197. goto Exit;
  198. rc = SQLSetDescField (hdesc, 2, SQL_DESC_LENGTH, (SQLPOINTER) BUFFER_LEN, 0);
  199. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 3 -- SQLSetDescField (SQL_DESC_LENGTH) failed for column fname\n"))
  200. goto Exit;
  201. rc = SQLSetDescField (hdesc, 2, SQL_DESC_OCTET_LENGTH, (SQLPOINTER)(BUFFER_LENW), 0);
  202. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 3 -- SQLSetDescField (SQL_DESC_OCTET_LENGTH) failed for column fname\n"))
  203. goto Exit;
  204. /* Setting descriptor fields for column - lname */
  205. rc = SQLSetDescFieldW (hdesc, 3, SQL_DESC_TYPE, (SQLPOINTER) SQL_C_WCHAR, 0);
  206. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 3 -- SQLSetDescField (SQL_DESC_DATA_TYPE) failed for column lname\n"))
  207. goto Exit;
  208. rc = SQLSetDescField (hdesc, 3, SQL_DESC_LENGTH, (SQLPOINTER) BUFFER_LEN, 0);
  209. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 3 -- SQLSetDescField (SQL_DESC_LENGTH) failed for column lname\n"))
  210. goto Exit;
  211. rc = SQLSetDescField (hdesc, 3, SQL_DESC_OCTET_LENGTH, (SQLPOINTER) (BUFFER_LENW), 0);
  212. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 3 -- SQLSetDescField (SQL_DESC_OCTET_LENGTH) failed for column lname\n"))
  213. goto Exit;
  214. fprintf (stdout, "STEP 3 done...descriptor fields set for all columns\nRetrieving data from table 'customer'\n\n");
  215. /* STEP 4. Set the explicitly allocated descriptor handle as the ARD for the select
  216. ** statement's handle
  217. ** Execute the SELECT statement
  218. ** Set the descriptor fields with the values returned by the SELECT statement
  219. ** Fetch the results
  220. ** Close the result set cursor
  221. */
  222. /* Set the explicitly allocated descriptor handle as the ARD for the select statement */
  223. rc = SQLSetStmtAttr (hSelectStmt, SQL_ATTR_APP_ROW_DESC, (SQLPOINTER) hdesc, 0);
  224. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step 4 -- SQLSetStmtAttr failed\n"))
  225. goto Exit;
  226. /* Execute the SELECT statement */
  227. rc = SQLExecDirectW (hSelectStmt, selectStmtW, SQL_NTS);
  228. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step 4 -- SQLExecDirect failed\n"))
  229. goto Exit;
  230. for (i = 0; i < 9; i++)
  231. {
  232. /* Set the descriptor fields for the values being returned */
  233. rc = SQLSetDescField (hdesc, 1, SQL_DESC_DATA_PTR, &(custnumArray[i]), sizeof(custnumArray[i])/*SQL_IS_INTEGER*/);
  234. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 4 -- SQLSetDescField failed for column cust_num\n"))
  235. goto Exit;
  236. rc = SQLSetDescFieldW (hdesc, 2, SQL_DESC_DATA_PTR, fnameArrayW[i], BUFFER_LEN);
  237. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 4 -- SQLSetDescField failed for column fname\n"))
  238. goto Exit;
  239. rc = SQLSetDescFieldW (hdesc, 3, SQL_DESC_DATA_PTR, lnameArrayW[i], BUFFER_LEN);
  240. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 4 -- SQLSetDescField failed for column lname\n"))
  241. goto Exit;
  242. /* Fetch the results */
  243. rc = SQLFetch (hSelectStmt);
  244. if (rc == SQL_NO_DATA_FOUND)
  245. break;
  246. else if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step 4 -- SQLFetch failed\n"))
  247. goto Exit;
  248. /* Display the results */
  249. fnameArray[i] = (SQLCHAR *) malloc (wcslen(fnameArrayW[i]) + sizeof(char));
  250. wcstombs( (char *) fnameArray[i], fnameArrayW[i], wcslen(fnameArrayW[i])
  251. + sizeof(char));
  252. lnameArray[i] = (SQLCHAR *) malloc (wcslen(lnameArrayW[i]) + sizeof(char));
  253. wcstombs( (char *) lnameArray[i], lnameArrayW[i], wcslen(lnameArrayW[i])
  254. + sizeof(char));
  255. fprintf (stdout, "Row retrieved from table 'customer' is - cust_num=%d, fname=%s, lname=%s\n",
  256. custnumArray[i], fnameArray[i], lnameArray[i]);
  257. }
  258. /* Close the result set cursor */
  259. rc = SQLCloseCursor (hSelectStmt);
  260. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step 4 -- SQLCloseCursor failed\n"))
  261. goto Exit;
  262. fprintf (stdout, "\n\nSTEP 4 done...SELECT statement executed...descriptor fields set with data retrieved\n");
  263. /* STEP 5. Set the explicitly allocated descriptor handle as the APD for the INSERT
  264. ** statement's handle
  265. ** Bind the input parameters for the INSERT statement
  266. ** For each row, set the SQL_DESC_DATA_PTR field to the value in the allocated
  267. ** arrays and execute the INSERT statement
  268. */
  269. /* Set the explicitly allocated descriptor handle as the APD for the INSERT statement */
  270. rc = SQLSetStmtAttr (hInsertStmt, SQL_ATTR_APP_PARAM_DESC, (SQLPOINTER) hdesc, 0);
  271. if (checkError (rc, SQL_HANDLE_STMT, hInsertStmt, (SQLCHAR *) "Error in Step 5 -- SQLSetStmtAttr failed\n"))
  272. goto Exit;
  273. /* Bind the input parameters for the INSERT statement */
  274. rc = SQLBindParameter (hInsertStmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER,
  275. 0, 0, &(custnumArray[i]), sizeof(custnumArray[i]), &cbCustnum);
  276. if (checkError (rc, SQL_HANDLE_STMT, hInsertStmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 1)\n"))
  277. goto Exit;
  278. rc = SQLBindParameter (hInsertStmt, 2, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_VARCHAR, BUFFER_LEN,
  279. 0, fnameArrayW[i], BUFFER_LENW, &cbFname);
  280. if (checkError (rc, SQL_HANDLE_STMT, hInsertStmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 2)\n"))
  281. goto Exit;
  282. rc = SQLBindParameter (hInsertStmt, 3, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_VARCHAR, BUFFER_LEN,
  283. 0, lnameArrayW[i], BUFFER_LENW, &cbLname);
  284. if (checkError (rc, SQL_HANDLE_STMT, hInsertStmt, (SQLCHAR *) "Error in Step 5 -- SQLBindParameter failed (param 3)\n"))
  285. goto Exit;
  286. /* For each row, set the SQL_DESC_DATA_PTR field and execute the INSERT statement */
  287. for (i = 0; i < 9; i++)
  288. {
  289. rc = SQLSetDescField (hdesc, 1, SQL_DESC_DATA_PTR, &(custnumArray[i]), sizeof(custnumArray[i]));
  290. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 5 -- SQLSetDescField failed for column cust_num\n"))
  291. goto Exit;
  292. rc = SQLSetDescFieldW (hdesc, 2, SQL_DESC_DATA_PTR, fnameArrayW[i], BUFFER_LEN);
  293. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 5 -- SQLSetDescField failed for column fname\n"))
  294. goto Exit;
  295. rc = SQLSetDescFieldW (hdesc, 3, SQL_DESC_DATA_PTR, lnameArrayW[i], BUFFER_LEN);
  296. if (checkError (rc, SQL_HANDLE_DESC, hdesc, (SQLCHAR *) "Error in Step 5 -- SQLSetDescField failed for column lname\n"))
  297. goto Exit;
  298. /* Execute the INSERT statement */
  299. rc = SQLExecDirectW (hInsertStmt, insertStmtW, SQL_NTS);
  300. if (checkError (rc, SQL_HANDLE_STMT, hInsertStmt, (SQLCHAR *) "Error in Step 5 -- SQLExecDirect failed\n"))
  301. goto Exit;
  302. }
  303. fprintf (stdout, "STEP 5 done...data inserted into table new_cust\n");
  304. fprintf (stdout, "\nRetrieving data from table 'new_cust'...\nHit <Enter> to continue...\n");
  305. in = getchar ();
  306. /* STEP 6. Execute the SELECT statement to retrieve data from
  307. ** table 'new_cust'
  308. ** Bind the result set columns
  309. ** Fetch and display the data
  310. */
  311. /* Execute the SELECT statement */
  312. rc = SQLExecDirectW (hSelectStmt, (SQLWCHAR *) L"SELECT * FROM new_cust", SQL_NTS);
  313. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step 6 -- SQLExecDirect failed\n"))
  314. goto Exit;
  315. /* Bind the result set columns */
  316. rc = SQLBindCol (hSelectStmt, 1, SQL_C_LONG, &newCustnum, sizeof(newCustnum), &cbCustnum);
  317. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step 6 -- SQLBindCol failed (column 1)\n"))
  318. goto Exit;
  319. rc = SQLBindCol (hSelectStmt, 2, SQL_C_WCHAR, newFnameW, BUFFER_LENW, &cbFname);
  320. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step 6 -- SQLBindCol failed (column 2)\n"))
  321. goto Exit;
  322. rc = SQLBindCol (hSelectStmt, 3, SQL_C_WCHAR, newLnameW, BUFFER_LENW, &cbLname);
  323. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step 6 -- SQLBindCol failed (column 3)\n"))
  324. goto Exit;
  325. /* Fetch and display the data retrieved */
  326. while (1)
  327. {
  328. /* Fetch the results */
  329. rc = SQLFetch (hSelectStmt);
  330. if (rc == SQL_NO_DATA_FOUND)
  331. break;
  332. else if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step 6 -- SQLFetch failed\n"))
  333. goto Exit;
  334. /* Display the results */
  335. newFname = (SQLCHAR *) malloc (wcslen(newFnameW) + sizeof(char));
  336. wcstombs( (char *) newFname, newFnameW, wcslen(newFnameW)
  337. + sizeof(char));
  338. newLname = (SQLCHAR *) malloc (wcslen(newLnameW) + sizeof(char));
  339. wcstombs( (char *) newLname, newLnameW, wcslen(newLnameW)
  340. + sizeof(char));
  341. fprintf (stdout, "Row retrieved from table 'new_cust' is - cust_num=%d, fname=%s, lname=%s\n", newCustnum, newFname, newLname);
  342. }
  343. /* Close the result set cursor */
  344. rc = SQLCloseCursor (hSelectStmt);
  345. if (checkError (rc, SQL_HANDLE_STMT, hSelectStmt, (SQLCHAR *) "Error in Step 6 -- SQLCloseCursor failed\n"))
  346. goto Exit;
  347. fprintf (stdout, "\n\nSTEP 6 done...SELECT statement executed...data retrieved from table 'new_cust'\n");
  348. Exit:
  349. /* CLEANUP: Drop table 'new_cust' from the database
  350. ** Close the statement handle
  351. ** Free the statement handle
  352. ** Disconnect from the datasource
  353. ** Free the connection and environment handles
  354. ** Exit
  355. */
  356. SQLExecDirectW (hTableStmt, (SQLWCHAR *) L"DROP TABLE new_cust", SQL_NTS);
  357. /* Close all the statement handles */
  358. SQLFreeStmt (hTableStmt, SQL_CLOSE);
  359. SQLFreeStmt (hSelectStmt, SQL_CLOSE);
  360. SQLFreeStmt (hInsertStmt, SQL_CLOSE);
  361. /* Free all the statement handles */
  362. SQLFreeHandle (SQL_HANDLE_STMT, hTableStmt);
  363. SQLFreeHandle (SQL_HANDLE_STMT, hSelectStmt);
  364. SQLFreeHandle (SQL_HANDLE_STMT, hInsertStmt);
  365. /* Free the descriptor handle */
  366. SQLFreeHandle (SQL_HANDLE_DESC, hdesc);
  367. /* Disconnect from the data source */
  368. SQLDisconnect (hdbc);
  369. /* Free the environment handle and the database connection handle */
  370. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  371. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  372. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  373. in = getchar ();
  374. return (rc);
  375. }