home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / gpre / dyn1.e < prev    next >
Encoding:
Text File  |  2001-01-05  |  3.1 KB  |  148 lines

  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.  *
  21.  * Copyright (C) 2000 Inprise Corporation
  22.  * All Rights Reserved.
  23.  * Contributor(s): ______________________________________.
  24.  */
  25.  
  26. #include "example.h"
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30. void pr_error (char *operation);
  31.  
  32. char    *new_dbname     = "new.gdb";
  33.  
  34. char    *create_tbl     = "CREATE TABLE dbinfo (when_created DATE)";
  35. char    *insert_date    = "INSERT INTO dbinfo VALUES ('NOW')";
  36.  
  37.  
  38. /*
  39.  *  Declare a database handle, which will be used by the new database.
  40.  */
  41. EXEC SQL
  42.     SET DATABASE db = COMPILETIME "employee.gdb";
  43.  
  44. int main(ARG(int, argc), ARG(char **, argv))
  45. ARGLIST(int argc)
  46. ARGLIST(char **argv)
  47. {
  48.  
  49.     db = NULL;
  50.  
  51.     /*
  52.      *  Create a new database, establishing a connection
  53.      *  as well.
  54.      */
  55.  
  56.     EXEC SQL
  57.         EXECUTE IMMEDIATE "CREATE DATABASE 'new.gdb'";
  58.  
  59.     if (SQLCODE)
  60.     {
  61.         /* Print a descriptive message, if the database exists. */
  62.         if (SQLCODE == -902)
  63.         {
  64.             printf("\nDatabase already exists.\n");
  65.             printf("Remove %s before running this program.\n\n", new_dbname);
  66.         }
  67.  
  68.         pr_error("create database");
  69.         return 1;
  70.     }
  71.  
  72.     EXEC SQL
  73.         COMMIT RELEASE;
  74.  
  75.     if (SQLCODE)
  76.     {
  77.         pr_error("commit & release");
  78.         return 1;
  79.     }
  80.  
  81.     printf("Created database '%s'.\n\n", new_dbname);
  82.  
  83.  
  84.     /*
  85.      *  Connect to the new database and create a sample table.
  86.      */
  87.  
  88.     /* Use the database handle declared above. */
  89.     EXEC SQL
  90.         CONNECT :new_dbname AS db;
  91.     if (SQLCODE)
  92.     {
  93.         pr_error("connect database");
  94.         return 1;
  95.     }
  96.  
  97.     /* Create a sample table. */
  98.     EXEC SQL
  99.         SET TRANSACTION;
  100.     EXEC SQL
  101.         EXECUTE IMMEDIATE :create_tbl;
  102.     if (SQLCODE)
  103.     {
  104.         pr_error("create table");
  105.         return 1;
  106.     }
  107.     EXEC SQL
  108.         COMMIT RETAIN;
  109.  
  110.     /* Insert 1 row into the new table. */
  111.     EXEC SQL
  112.         SET TRANSACTION;
  113.     EXEC SQL
  114.         EXECUTE IMMEDIATE :insert_date;
  115.     if (SQLCODE)
  116.     {
  117.         pr_error("insert into");
  118.         return 1;
  119.     }
  120.     EXEC SQL
  121.         COMMIT RELEASE;
  122.  
  123.     printf("Successfully accessed the newly created database.\n\n");
  124.  
  125.     EXEC SQL
  126.         DISCONNECT db;
  127.  
  128.     return 0;
  129. }
  130.  
  131.  
  132. /*
  133.  *    Print the status, the SQLCODE, and exit.
  134.  *    Also, indicate which operation the error occured on.
  135.  */
  136. void pr_error(ARG(char *, operation))
  137. ARGLIST(char *operation)
  138. {
  139.     printf("[\n");
  140.     printf("PROBLEM ON \"%s\".\n", operation);
  141.          
  142.     isc_print_status(gds__status);
  143.  
  144.     printf("SQLCODE = %d\n", SQLCODE);
  145.     
  146.     printf("]\n");
  147. }
  148.