home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Alu / Ancestrologie / Sources / InterBase_WI-V6.0.1-server.ZIP / examples / api / api1.c < prev    next >
C/C++ Source or Header  |  2001-01-05  |  5KB  |  143 lines

  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.  *
  24.  * Copyright (C) 2000 Inprise Corporation
  25.  * All Rights Reserved.
  26.  * Contributor(s): ______________________________________.
  27.  */
  28.  
  29.  
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include "example.h"
  34. #include <ibase.h>
  35.  
  36. int pr_error (long ISC_FAR *, char ISC_FAR *);
  37.  
  38.  
  39.  
  40. static char ISC_FAR *create_tbl  = "CREATE TABLE dbinfo (when_created DATE)";
  41. static char ISC_FAR *insert_date = "INSERT INTO dbinfo VALUES ('NOW')";
  42.  
  43. int main (ARG(int, argc), ARG(char **, argv))
  44. ARGLIST(int argc)
  45. ARGLIST(char **argv) 
  46. {
  47.     isc_db_handle   newdb = NULL;          /* database handle */
  48.     isc_tr_handle   trans = NULL;          /* transaction handle */
  49.     long            status[20];            /* status vector */
  50.     long            sqlcode;               /* SQLCODE  */
  51.     char            create_db[160];        /* 'create database' statement */
  52.     char            new_dbname[128];
  53.  
  54.     if (argc > 1)
  55.         strcpy(new_dbname, argv[1]);
  56.     else
  57.         strcpy(new_dbname, "new.gdb");
  58.  
  59.     /*
  60.      *    Construct a 'create database' statement.
  61.      *    The database name could have been passed as a parameter.
  62.      */
  63.     sprintf(create_db, "CREATE DATABASE '%s'", new_dbname);
  64.     
  65.     /*
  66.      *    Create a new database.
  67.      *    The database handle is zero.
  68.      */
  69.     
  70.     if (isc_dsql_execute_immediate(status, &newdb, &trans, 0, create_db, 1,
  71.                                    NULL))
  72.     {
  73.         /* Extract SQLCODE from the status vector. */
  74.         sqlcode = isc_sqlcode(status);
  75.  
  76.         /* Print a descriptive message based on the SQLCODE. */
  77.         if (sqlcode == -902)
  78.         {
  79.             printf("\nDatabase already exists.\n");
  80.             printf("Remove %s before running this program.\n\n", new_dbname);
  81.         }
  82.  
  83.         /* In addition, print a standard error message. */
  84.         if (pr_error(status, "create database"))
  85.             return 1;
  86.     }
  87.  
  88.     isc_commit_transaction(status, &trans);
  89.     printf("Created database '%s'.\n\n", new_dbname);
  90.  
  91.     /*
  92.      *    Connect to the new database and create a sample table.
  93.      */
  94.  
  95.     /* newdb will be set to null on success */ 
  96.     isc_detach_database(status, &newdb);
  97.  
  98.     if (isc_attach_database(status, 0, new_dbname, &newdb, 0, NULL))
  99.         if (pr_error(status, "attach database"))
  100.             return 1;
  101.  
  102.  
  103.     /* Create a sample table. */
  104.     isc_start_transaction(status, &trans, 1, &newdb, 0, NULL);
  105.     if (isc_dsql_execute_immediate(status, &newdb, &trans, 0, create_tbl, 1, NULL))
  106.         if (pr_error(status, "create table"))
  107.             return 1;
  108.     isc_commit_transaction(status, &trans);
  109.  
  110.     /* Insert 1 row into the new table. */
  111.     isc_start_transaction(status, &trans, 1, &newdb, 0, NULL);
  112.     if (isc_dsql_execute_immediate(status, &newdb, &trans, 0, insert_date, 1, NULL))
  113.         if (pr_error(status, "insert into"))
  114.             return 1;
  115.     isc_commit_transaction(status, &trans);
  116.  
  117.     printf("Successfully accessed the newly created database.\n\n");
  118.  
  119.     isc_detach_database(status, &newdb);
  120.  
  121.     return 0;
  122. }            
  123.  
  124. /*
  125.  *    Print the status, the SQLCODE, and exit.
  126.  *    Also, indicate which operation the error occured on.
  127.  */
  128. int pr_error (ARG(long ISC_FAR *, status), ARG(char ISC_FAR *, operation))
  129. ARGLIST(long ISC_FAR * status)
  130. ARGLIST(char ISC_FAR * operation)                                         
  131. {
  132.     printf("[\n");
  133.     printf("PROBLEM ON \"%s\".\n", operation);
  134.  
  135.     isc_print_status(status);
  136.  
  137.     printf("SQLCODE:%d\n", isc_sqlcode(status));
  138.  
  139.     printf("]\n");
  140.  
  141.     return 1;
  142. }
  143.