posupdt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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: posupdt.c
  14. *
  15. * Description: To use SQLSetPos for positional updates to a result set
  16. *
  17. *
  18. ***************************************************************************
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #ifndef NO_WIN32
  24. #include <io.h>
  25. #include <windows.h>
  26. #include <conio.h>
  27. #endif /*NO_WIN32*/
  28. #include "infxcli.h"
  29. #define BUFFER_LEN 10
  30. #define ERRMSG_LEN 200
  31. #define NUM_ROWS 5
  32. SQLCHAR defDsn[] = "odbc_demo";
  33. SQLINTEGER checkError (SQLRETURN rc,
  34. SQLSMALLINT handleType,
  35. SQLHANDLE handle,
  36. SQLCHAR* errmsg)
  37. {
  38. SQLRETURN retcode = SQL_SUCCESS;
  39. SQLSMALLINT errNum = 1;
  40. SQLCHAR sqlState[6];
  41. SQLINTEGER nativeError;
  42. SQLCHAR errMsg[ERRMSG_LEN];
  43. SQLSMALLINT textLengthPtr;
  44. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  45. {
  46. while (retcode != SQL_NO_DATA)
  47. {
  48. retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
  49. if (retcode == SQL_INVALID_HANDLE)
  50. {
  51. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  52. return 1;
  53. }
  54. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  55. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  56. errNum++;
  57. }
  58. fprintf (stderr, "%s\n", errmsg);
  59. return 1; /* all errors on this handle have been reported */
  60. }
  61. else
  62. return 0; /* no errors to report */
  63. }
  64. int main (long argc,
  65. char* argv[])
  66. {
  67. /* Declare variables
  68. */
  69. /* Handles */
  70. SQLHDBC hdbc;
  71. SQLHENV henv;
  72. SQLHSTMT hstmt;
  73. SQLHSTMT hNewStmt;
  74. /* Miscellaneous variables */
  75. SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
  76. SQLRETURN rc = 0;
  77. SQLINTEGER in;
  78. SQLCHAR* selectStmt = (SQLCHAR *) "SELECT cust_num, fname, lname FROM customer";
  79. SQLCHAR* newLname = (SQLCHAR *) "Miller";
  80. SQLCHAR fname[NUM_ROWS][BUFFER_LEN], lname[NUM_ROWS][BUFFER_LEN];
  81. SQLCHAR newfname[NUM_ROWS][BUFFER_LEN], newlname[NUM_ROWS][BUFFER_LEN];
  82. SQLINTEGER cust_num[NUM_ROWS];
  83. SQLLEN cbCustNum[NUM_ROWS], cbFname[NUM_ROWS], cbLname[NUM_ROWS];
  84. SQLINTEGER newcust_num[NUM_ROWS];
  85. SQLLEN newcbCustNum[NUM_ROWS], newcbFname[NUM_ROWS], newcbLname[NUM_ROWS];
  86. SQLUSMALLINT i, statusArray[NUM_ROWS];
  87. /* STEP 1. Get data source name from command line (or use default)
  88. ** Allocate the environment handle and set ODBC version
  89. ** Allocate the connection handle
  90. ** Establish the database connection
  91. ** Set the connection attribute to enable scrollable cursors
  92. ** Allocate the statement handles
  93. */
  94. /* If(dsn is not explicitly passed in as arg) */
  95. if (argc != 2)
  96. {
  97. /* Use default dsn - odbc_demo */
  98. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  99. strcpy ((char *)dsn, (char *)defDsn);
  100. }
  101. else
  102. {
  103. /* Use specified dsn */
  104. strcpy ((char *)dsn, (char *)argv[1]);
  105. fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
  106. }
  107. /* Allocate the Environment handle */
  108. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  109. if (rc != SQL_SUCCESS)
  110. {
  111. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  112. return (1);
  113. }
  114. /* Set the ODBC version to 3.0 */
  115. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  116. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  117. return (1);
  118. /* Allocate the connection handle */
  119. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  120. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR *) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  121. return (1);
  122. /* Establish the database connection */
  123. rc = SQLConnect (hdbc, dsn, SQL_NTS, (SQLCHAR *) "", SQL_NTS, (SQLCHAR *) "", SQL_NTS);
  124. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- SQLConnect failed\nExiting!!"))
  125. return (1);
  126. /* Set the connection attribute to enable scrollable cursors */
  127. rc = SQLSetConnectAttr (hdbc, SQL_INFX_ATTR_ENABLE_SCROLL_CURSORS, (SQLPOINTER) SQL_TRUE, (SQLINTEGER) NULL);
  128. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Set Connection Attribute failed\nExiting!!"))
  129. return (1);
  130. /* Allocate the statement handles */
  131. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  132. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  133. return (1);
  134. fprintf (stdout, "STEP 1 done...connected to database\n");
  135. /* STEP 2 Set the following statement attributes
  136. ** -- SQL_ATTR_ROW_ARRAY_SIZE to the number of rows to be bound
  137. ** SQL_ATTR_CONCURRENCY to SQL_CONCUR_LOCK
  138. ** SQL_ATTR_CURSOR_TYPE to SQL_CURSOR_STATIC
  139. ** SQL_ATTR_ROW_STATUS_PTR tot he address of the status array
  140. */
  141. /* Set the statement attribute SQL_ATTR_ROW_ARRAY_SIZE to the number of rows to be bound */
  142. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) NUM_ROWS, 0);
  143. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_ARRAY_SIZE\n"))
  144. goto Exit;
  145. /* Set the statement attribute SQL_ATTR_CONCURRENCY to SQL_CONCUR_LOCK */
  146. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_LOCK, 0);
  147. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_CONCURRENCY\n"))
  148. goto Exit;
  149. /* Set the statement attribute SQL_ATTR_CURSOR_TYPE to SQL_CURSOR_STATIC */
  150. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_STATIC, 0);
  151. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_CURSOR_TYPE\n"))
  152. goto Exit;
  153. /* Set the statement attribute SQL_ATTR_ROW_STATUS_PTR to the status array */
  154. rc = SQLSetStmtAttr (hstmt, SQL_ATTR_ROW_STATUS_PTR, statusArray, 0);
  155. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_STATUS_PTR\n"))
  156. goto Exit;
  157. fprintf (stdout, "STEP 2 done...Statement attributes set\n");
  158. /* STEP 3. Retrieve data from the 'customer' table
  159. ** Display the results
  160. */
  161. /* Retrieve data from the 'customer' table */
  162. fprintf (stdout, "Retrieving data from 'customer' table\n\n");
  163. rc = SQLExecDirect (hstmt, selectStmt, SQL_NTS);
  164. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLExecDirect failed\n"))
  165. goto Exit;
  166. /* Bind the result set columns */
  167. rc = SQLBindCol (hstmt, 1, SQL_C_LONG, &cust_num[0], 0, &cbCustNum[0]);
  168. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 1)\n"))
  169. goto Exit;
  170. rc = SQLBindCol (hstmt, 2, SQL_C_CHAR, fname[0], BUFFER_LEN, &cbFname[0]);
  171. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 2)\n"))
  172. goto Exit;
  173. rc = SQLBindCol (hstmt, 3, SQL_C_CHAR, lname[0], BUFFER_LEN, &cbLname[0]);
  174. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLBindCol failed (column 3)\n"))
  175. goto Exit;
  176. /* Fetch the results */
  177. rc = SQLFetch (hstmt);
  178. if (rc == SQL_NO_DATA_FOUND)
  179. fprintf (stdout, "NO DATA FOUND!!\n EXITING!!");
  180. else if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 3 -- SQLFetch failed\n"))
  181. goto Exit;
  182. for (i=0; i<NUM_ROWS; i++)
  183. {
  184. /* Display the results */
  185. fprintf (stdout, "Cust Num: %d, Fname: %s, Lname: %s\n", cust_num[i], fname[i], lname[i]);
  186. }
  187. fprintf (stdout, "\nSTEP 3 done...original data retrieved from 'customer' table\n");
  188. /* STEP 4. Update the 'lname' field of the 'customer' table for cust_num = 102
  189. ** Call SQLSetPos to update the data in the table
  190. ** Call SQLEndTran to commit the transaction
  191. */
  192. /* Update the value in the 'lname' array where cust_num = 102 to 'Miller' */
  193. for (i=0; i< NUM_ROWS; i++)
  194. {
  195. if (cust_num[i] == 102)
  196. {
  197. strcpy ((char*) lname[i], (char*) newLname);
  198. cbLname[i] = strlen((char *)newLname);
  199. /* Call SQLSetPos to update the data */
  200. rc = SQLSetPos (hstmt, (SQLUSMALLINT) (i+1), (SQLUSMALLINT) SQL_UPDATE, (SQLUSMALLINT) SQL_LOCK_NO_CHANGE);
  201. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLSetPos\n"))
  202. goto Exit;
  203. break;
  204. }
  205. }
  206. /* Commit the transaction */
  207. rc = SQLEndTran (SQL_HANDLE_DBC, hdbc, SQL_COMMIT);
  208. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR *) "Error in Step 4 -- SQLEndtran\n"))
  209. goto Exit;
  210. fprintf (stdout, "STEP 4 done...row updated using SQLSetPos\n");
  211. /* STEP 5. Retrieve updated data from the 'customer' table
  212. ** Display the results
  213. */
  214. /* Retrieve updated data from the 'customer' table */
  215. fprintf (stdout, "Retrieving data from 'customer' table\n\n");
  216. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hNewStmt );
  217. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR *) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  218. return (1);
  219. /* Set the statement attribute SQL_ATTR_ROW_ARRAY_SIZE to the number of rows to be bound */
  220. rc = SQLSetStmtAttr (hNewStmt, SQL_ATTR_ROW_ARRAY_SIZE, (SQLPOINTER) NUM_ROWS, 0);
  221. if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 2 -- SQLSetStmtAttr failed for SQL_ATTR_ROW_ARRAY_SIZE\n"))
  222. goto Exit;
  223. rc = SQLExecDirect (hNewStmt, selectStmt, SQL_NTS);
  224. if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 5 -- SQLExecDirect failed\n"))
  225. goto Exit;
  226. /* Bind the result set columns */
  227. rc = SQLBindCol (hNewStmt, 1, SQL_C_LONG, &newcust_num[0], 0, &newcbCustNum[0]);
  228. if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 5 -- SQLBindCol failed (column 1)\n"))
  229. goto Exit;
  230. rc = SQLBindCol (hNewStmt, 2, SQL_C_CHAR, newfname[0], BUFFER_LEN, &newcbFname[0]);
  231. if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 5 -- SQLBindCol failed (column 2)\n"))
  232. goto Exit;
  233. rc = SQLBindCol (hNewStmt, 3, SQL_C_CHAR, newlname[0], BUFFER_LEN, &newcbLname[0]);
  234. if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 5 -- SQLBindCol failed (column 3)\n"))
  235. goto Exit;
  236. /* Fetch the results */
  237. rc = SQLFetch (hNewStmt);
  238. if (rc == SQL_NO_DATA_FOUND)
  239. fprintf (stdout, "NO DATA FOUND!!\n EXITING!!");
  240. else if (checkError (rc, SQL_HANDLE_STMT, hNewStmt, (SQLCHAR *) "Error in Step 5 -- SQLFetch failed\n"))
  241. goto Exit;
  242. for (i=0; i<NUM_ROWS; i++)
  243. {
  244. /* Display the results */
  245. fprintf (stdout, "Cust Num: %d, Fname: %s, Lname: %s\n", newcust_num[i], newfname[i], newlname[i]);
  246. }
  247. fprintf (stdout, "\nSTEP 5 done...updated data retrieved from 'customer' table\n");
  248. Exit:
  249. /* CLEANUP: Close the statement handle
  250. ** Free the statement handle
  251. ** Disconnect from the datasource
  252. ** Free the connection and environment handles
  253. ** Exit
  254. */
  255. /* Close the statement handle */
  256. SQLFreeStmt (hstmt, SQL_CLOSE);
  257. SQLFreeStmt (hNewStmt, SQL_CLOSE);
  258. /* Free the statement handle */
  259. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  260. SQLFreeHandle (SQL_HANDLE_STMT, hNewStmt);
  261. /* Disconnect from the data source */
  262. SQLDisconnect (hdbc);
  263. /* Free the environment handle and the database connection handle */
  264. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  265. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  266. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  267. in = getchar ();
  268. return (rc);
  269. }