dyn1.e 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Program type: Embedded Dynamic SQL
  3. *
  4. * Description:
  5. * This program creates a new database, using a static SQL string.
  6. * The newly created database is accessed after its creation,
  7. * and a sample table is added.
  8. * The contents of this file are subject to the Interbase Public
  9. * License Version 1.0 (the "License"); you may not use this file
  10. * except in compliance with the License. You may obtain a copy
  11. * of the License at http://www.Inprise.com/IPL.html
  12. *
  13. * Software distributed under the License is distributed on an
  14. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  15. * or implied. See the License for the specific language governing
  16. * rights and limitations under the License.
  17. *
  18. * The Original Code was created by Inprise Corporation
  19. * and its predecessors. Portions created by Inprise Corporation are
  20. * Copyright (C) Inprise Corporation.
  21. *
  22. * All Rights Reserved.
  23. * Contributor(s): ______________________________________.
  24. */
  25. #include "example.h"
  26. #include <stdlib.h>
  27. #include <string.h>
  28. void pr_error (char *operation);
  29. char *new_dbname = "new.fdb";
  30. char *create_tbl = "CREATE TABLE dbinfo (when_created DATE)";
  31. char *insert_date = "INSERT INTO dbinfo VALUES ('NOW')";
  32. /*
  33. * Declare a database handle, which will be used by the new database.
  34. */
  35. EXEC SQL
  36. SET DATABASE db = COMPILETIME "employee.fdb";
  37. int main(int argc, char** argv)
  38. {
  39. db = NULL;
  40. /*
  41. * Create a new database, establishing a connection
  42. * as well.
  43. */
  44. EXEC SQL
  45. EXECUTE IMMEDIATE "CREATE DATABASE 'new.fdb'";
  46. if (SQLCODE)
  47. {
  48. /* Print a descriptive message, if the database exists. */
  49. if (SQLCODE == -902)
  50. {
  51. printf("\nDatabase already exists.\n");
  52. printf("Remove %s before running this program.\n\n", new_dbname);
  53. }
  54. pr_error("create database");
  55. return 1;
  56. }
  57. EXEC SQL
  58. COMMIT RELEASE;
  59. if (SQLCODE)
  60. {
  61. pr_error("commit & release");
  62. return 1;
  63. }
  64. printf("Created database '%s'.\n\n", new_dbname);
  65. /*
  66. * Connect to the new database and create a sample table.
  67. */
  68. /* Use the database handle declared above. */
  69. EXEC SQL
  70. CONNECT :new_dbname AS db;
  71. if (SQLCODE)
  72. {
  73. pr_error("connect database");
  74. return 1;
  75. }
  76. /* Create a sample table. */
  77. EXEC SQL
  78. SET TRANSACTION;
  79. EXEC SQL
  80. EXECUTE IMMEDIATE :create_tbl;
  81. if (SQLCODE)
  82. {
  83. pr_error("create table");
  84. return 1;
  85. }
  86. EXEC SQL
  87. COMMIT RETAIN;
  88. /* Insert 1 row into the new table. */
  89. EXEC SQL
  90. SET TRANSACTION;
  91. EXEC SQL
  92. EXECUTE IMMEDIATE :insert_date;
  93. if (SQLCODE)
  94. {
  95. pr_error("insert into");
  96. return 1;
  97. }
  98. EXEC SQL
  99. COMMIT RELEASE;
  100. printf("Successfully accessed the newly created database.\n\n");
  101. EXEC SQL
  102. DISCONNECT db;
  103. return 0;
  104. }
  105. /*
  106. * Print the status, the SQLCODE, and exit.
  107. * Also, indicate which operation the error occurred on.
  108. */
  109. void pr_error(char* operation)
  110. {
  111. printf("[\n");
  112. printf("PROBLEM ON \"%s\".\n", operation);
  113. isc_print_status(gds__status);
  114. printf("SQLCODE = %d\n", SQLCODE);
  115. printf("]\n");
  116. }