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