dbdrop.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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: dbdrop.c
  14. *
  15. * Description: To drop the sample database (created by dbCreate.c)
  16. * Before running this program, make sure that all
  17. * connections to the sample database have been closed.
  18. * To successfully drop the sample database, you must
  19. * have DBA permissions or be running this as user
  20. * 'informix'
  21. *
  22. ***************************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #ifndef NO_WIN32
  28. #include <io.h>
  29. #include <windows.h>
  30. #include <conio.h>
  31. #endif /*NO_WIN32*/
  32. #include "infxcli.h"
  33. #define BUFFER_LEN 70
  34. #define ERRMSG_LEN 200
  35. SQLCHAR defDsn[] = "odbc_demo";
  36. SQLINTEGER checkError (SQLRETURN rc,
  37. SQLSMALLINT handleType,
  38. SQLHANDLE handle,
  39. SQLCHAR* errmsg)
  40. {
  41. SQLRETURN retcode = SQL_SUCCESS;
  42. SQLSMALLINT errNum = 1;
  43. SQLCHAR sqlState[6];
  44. SQLINTEGER nativeError;
  45. SQLCHAR errMsg[ERRMSG_LEN];
  46. SQLSMALLINT textLengthPtr;
  47. if ((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO))
  48. {
  49. while (retcode != SQL_NO_DATA)
  50. {
  51. retcode = SQLGetDiagRec (handleType, handle, errNum, sqlState, &nativeError, errMsg, ERRMSG_LEN, &textLengthPtr);
  52. if (retcode == SQL_INVALID_HANDLE)
  53. {
  54. fprintf (stderr, "checkError function was called with an invalid handle!!\n");
  55. return 1;
  56. }
  57. if ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
  58. fprintf (stderr, "ERROR: %d: %s : %s \n", nativeError, sqlState, errMsg);
  59. errNum++;
  60. }
  61. fprintf (stderr, "%s\n", errmsg);
  62. return 1; /* all errors on this handle have been reported */
  63. }
  64. else
  65. return 0; /* no errors to report */
  66. }
  67. int main (long argc,
  68. char* argv[])
  69. {
  70. /* Declare variables
  71. */
  72. /* Handles */
  73. SQLHDBC hdbc;
  74. SQLHENV henv;
  75. SQLHSTMT hstmt;
  76. /* Miscellaneous variables */
  77. SQLCHAR dsn[20]; /*name of the DSN used for connecting to the database*/
  78. SQLRETURN rc = 0;
  79. SQLINTEGER in;
  80. SQLCHAR connStrIn[BUFFER_LEN];
  81. SQLCHAR connStrOut[BUFFER_LEN];
  82. SQLSMALLINT connStrOutLen;
  83. SQLCHAR* dropDBStmt = (SQLCHAR *) "DROP DATABASE odbc_demodb";
  84. /* STEP 1. Get data source name from command line (or use default)
  85. ** Allocate the environment handle and set ODBC version
  86. ** Allocate the connection handle
  87. ** Establish the database connection
  88. ** Allocate the statement handle
  89. */
  90. /* If(dsn is not explicitly passed in as arg) */
  91. if (argc != 2)
  92. {
  93. /* Use default dsn - odbc_demo */
  94. fprintf (stdout, "\nUsing default DSN : %s\n", defDsn);
  95. strcpy ((char *)dsn, (char *)defDsn);
  96. }
  97. else
  98. {
  99. /* Use specified dsn */
  100. strcpy ((char *)dsn, (char *)argv[1]);
  101. fprintf (stdout, "\nUsing specified DSN : %s\n", dsn);
  102. }
  103. /* Allocate the Environment handle */
  104. rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  105. if (rc != SQL_SUCCESS)
  106. {
  107. fprintf (stdout, "Environment Handle Allocation failed\nExiting!!");
  108. return (1);
  109. }
  110. /* Set the ODBC version to 3.0 */
  111. rc = SQLSetEnvAttr (henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER) SQL_OV_ODBC3, 0);
  112. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR * ) "Error in Step 1 -- SQLSetEnvAttr failed\nExiting!!"))
  113. return (1);
  114. /* Allocate the connection handle */
  115. rc = SQLAllocHandle (SQL_HANDLE_DBC, henv, &hdbc);
  116. if (checkError (rc, SQL_HANDLE_ENV, henv, (SQLCHAR * ) "Error in Step 1 -- Connection Handle Allocation failed\nExiting!!"))
  117. return (1);
  118. /* Establish the database connection */
  119. sprintf((char *) connStrIn, "DSN=%s;connectdatabase=NO", dsn);
  120. rc = SQLDriverConnect (hdbc, NULL, connStrIn, SQL_NTS, connStrOut, BUFFER_LEN, &connStrOutLen, SQL_DRIVER_NOPROMPT);
  121. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR * ) "Error in Step 1 -- SQLDriverConnect failed\nExiting!!"))
  122. return (1);
  123. /* Allocate the statement handle */
  124. rc = SQLAllocHandle (SQL_HANDLE_STMT, hdbc, &hstmt );
  125. if (checkError (rc, SQL_HANDLE_DBC, hdbc, (SQLCHAR * ) "Error in Step 1 -- Statement Handle Allocation failed\nExiting!!"))
  126. return (1);
  127. fprintf (stdout, "STEP 1 done...connected to database\n");
  128. /* STEP 2. Drop the sample database -- odbc_demodb */
  129. rc = SQLExecDirect (hstmt, dropDBStmt, SQL_NTS);
  130. if (checkError (rc, SQL_HANDLE_STMT, hstmt, (SQLCHAR * ) "Error in Step 2 -- SQLExecDirect failed\n"))
  131. goto Exit;
  132. fprintf(stdout, "STEP 2 done...Sample Database Dropped\n");
  133. Exit:
  134. /* CLEANUP: Close the statement handle
  135. ** Free the statement handle
  136. ** Disconnect from the datasource
  137. ** Free the connection and environment handles
  138. ** Exit
  139. */
  140. /* Close the statement handle */
  141. SQLFreeStmt (hstmt, SQL_CLOSE);
  142. /* Free the statement handle */
  143. SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
  144. /* Disconnect from the data source */
  145. SQLDisconnect (hdbc);
  146. /* Free the environment handle and the database connection handle */
  147. SQLFreeHandle (SQL_HANDLE_DBC, hdbc);
  148. SQLFreeHandle (SQL_HANDLE_ENV, henv);
  149. fprintf (stdout,"\n\nHit <Enter> to terminate the program...\n\n");
  150. in = getchar ();
  151. return (rc);
  152. }