api1.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Program type: API Interface
  3. *
  4. * Description:
  5. * This program creates a new database, given an SQL statement
  6. * string. The newly created database is accessed after its
  7. * creation, and a sample table is added.
  8. *
  9. * The SQLCODE is extracted from the status vector and is used
  10. * to check whether the database already exists.
  11. * The contents of this file are subject to the Interbase Public
  12. * License Version 1.0 (the "License"); you may not use this file
  13. * except in compliance with the License. You may obtain a copy
  14. * of the License at http://www.Inprise.com/IPL.html
  15. *
  16. * Software distributed under the License is distributed on an
  17. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  18. * or implied. See the License for the specific language governing
  19. * rights and limitations under the License.
  20. *
  21. * The Original Code was created by Inprise Corporation
  22. * and its predecessors. Portions created by Inprise Corporation are
  23. * Copyright (C) Inprise Corporation.
  24. *
  25. * All Rights Reserved.
  26. * Contributor(s): ______________________________________.
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdio.h>
  31. #include "example.h"
  32. #include <ibase.h>
  33. int pr_error (long *, char *);
  34. static char *create_tbl = "CREATE TABLE dbinfo (when_created DATE)";
  35. static char *insert_date = "INSERT INTO dbinfo VALUES ('NOW')";
  36. int main (int argc, char** argv)
  37. {
  38. isc_db_handle newdb = NULL; /* database handle */
  39. isc_tr_handle trans = NULL; /* transaction handle */
  40. ISC_STATUS_ARRAY status; /* status vector */
  41. long sqlcode; /* SQLCODE */
  42. char create_db[160]; /* 'create database' statement */
  43. char new_dbname[128];
  44. if (argc > 1)
  45. strcpy(new_dbname, argv[1]);
  46. else
  47. strcpy(new_dbname, "new.fdb");
  48. /*
  49. * Construct a 'create database' statement.
  50. * The database name could have been passed as a parameter.
  51. */
  52. sprintf(create_db, "CREATE DATABASE '%s'", new_dbname);
  53. /*
  54. * Create a new database.
  55. * The database handle is zero.
  56. */
  57. if (isc_dsql_execute_immediate(status, &newdb, &trans, 0, create_db, 1,
  58. NULL))
  59. {
  60. /* Extract SQLCODE from the status vector. */
  61. sqlcode = isc_sqlcode(status);
  62. /* Print a descriptive message based on the SQLCODE. */
  63. if (sqlcode == -902)
  64. {
  65. printf("\nDatabase already exists.\n");
  66. printf("Remove %s before running this program.\n\n", new_dbname);
  67. }
  68. /* In addition, print a standard error message. */
  69. if (pr_error(status, "create database"))
  70. return 1;
  71. }
  72. isc_commit_transaction(status, &trans);
  73. printf("Created database '%s'.\n\n", new_dbname);
  74. /*
  75. * Connect to the new database and create a sample table.
  76. */
  77. /* newdb will be set to null on success */
  78. isc_detach_database(status, &newdb);
  79. if (isc_attach_database(status, 0, new_dbname, &newdb, 0, NULL))
  80. if (pr_error(status, "attach database"))
  81. return 1;
  82. /* Create a sample table. */
  83. isc_start_transaction(status, &trans, 1, &newdb, 0, NULL);
  84. if (isc_dsql_execute_immediate(status, &newdb, &trans, 0, create_tbl, 1, NULL))
  85. if (pr_error(status, "create table"))
  86. return 1;
  87. isc_commit_transaction(status, &trans);
  88. /* Insert 1 row into the new table. */
  89. isc_start_transaction(status, &trans, 1, &newdb, 0, NULL);
  90. if (isc_dsql_execute_immediate(status, &newdb, &trans, 0, insert_date, 1, NULL))
  91. if (pr_error(status, "insert into"))
  92. return 1;
  93. isc_commit_transaction(status, &trans);
  94. printf("Successfully accessed the newly created database.\n\n");
  95. isc_detach_database(status, &newdb);
  96. return 0;
  97. }
  98. /*
  99. * Print the status, the SQLCODE, and exit.
  100. * Also, indicate which operation the error occured on.
  101. */
  102. int pr_error (long* status, char* operation)
  103. {
  104. printf("[\n");
  105. printf("PROBLEM ON \"%s\".\n", operation);
  106. isc_print_status(status);
  107. printf("SQLCODE:%d\n", isc_sqlcode(status));
  108. printf("]\n");
  109. return 1;
  110. }