René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Creating a table with adpoci [adpoci example]

Here's an OCI programm that creates a table. After creating the table, fill the table with insert_table.
This programm needs adpoci to be linked.
Be sure to first compile and link the library into a shared library or dll.
After doing so, the programm can be compiled and linked like so
On Linux with gcc:
gcc -I<a href='/ora/misc/env_vars.html#oracle_home")'>$ORACLE_HOME</a>/rdbms/demo -I$ORACLE_HOME/rdbms/public create_table.c -o create_table -L. -L$ORACLE_HOME/lib -ladpoci -lclntsh
On Windows with mingw:
gcc -Wall -I%ORACLE_HOME%/oci/include create_table.c adpoci.dll %ORACLE_HOME%/bin/oci.dll -o create_table.exe
With cygwin:
gcc -I$ORACLE_HOME/oci/include -I../adpoci/ create_table.c -o create_table -L../adpoci -L$ORACLE_HOME/lib -ladpoci $ORACLE_HOME/bin/oci.dll -mno-cygwin

Usage

./create_table username/password@db

create_table.c

create_table.c
#include <stdio.h>
#include <stdlib.h>

#include "adpoci.h"

int main(int argc, char* argv[]) {
         char err_msg   [512];
         sword          r;
  struct oci_connection conn;

  if (argc < 2) {
    printf("usage %s username/password[@dbname]\n", argv[0]);
    exit (-1);
  }

  text  username[30];
  text  password[30];
  text  dbname  [30];

  parse_connect_string(argv[1],username, password, dbname);

  if (!oci_connect(username, password, dbname, &conn, err_msg)) {
    printf("Error with oci_connect: %s\n",err_msg);
    goto clean_up;
  }

  if (!oci_parse_and_execute("create table oci_test_table ( "
                             "  number_      number,        "
                             "  number_5_3   number(5,3),   "
                             "  varchar2_100 varchar2(100), "
                             "  blob_        blob,          "
                             "  clob_        clob,          "
                             "  date_        date)          ",
                             err_msg, &conn) ) {
    printf(err_msg);
    goto clean_up;
  }

  printf("A table named oci_test_table should now be created\n");

clean_up:

  OCITerminate(OCI_DEFAULT);

  return 0;
}