ex13.c

Example for inserting data into a blob column from an internal buffer.

00001 /* $Id: ex13.c 286 2004-06-15 10:15:52Z kpoitschke $ */
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include "examples.h"
00005 
00006 
00007 int insert_into_blob_table(sqlo_db_handle_t dbh, int key )
00008 {
00009   char * stmt = 
00010     "INSERT INTO T_SQLORA_BLOB (KEY, CDATA) "
00011     "VALUES (:b1, EMPTY_CLOB()) RETURNING CDATA INTO :b2";
00012 
00013   char data[MAX_BLOB_BUFFER_DATA];        /* a data buffer */
00014   sqlo_lob_desc_t loblp;                  /* the lob locator */
00015   sqlo_stmt_handle_t sth;
00016   int status;
00017   int k = key;
00018 
00019   printf("Insert CLOB\n");
00020 
00021   /* create the test table */
00022   create_blob_table(dbh);
00023 
00024   /* fill the data buffer with some characters */
00025   fillbuf(data, MAX_BLOB_BUFFER_DATA);
00026 
00027   /* parse the statement */
00028 
00029   if (0>(sth = sqlo_prepare(dbh, stmt)))
00030     error_exit(dbh, "sqlo_prepare");
00031 
00032   /* alloate the lob descriptor */
00033   if (0 > sqlo_alloc_lob_desc(dbh, &loblp))
00034     error_exit(dbh, "sqlo_alloc_lob_desc");
00035 
00036   /* bind input variables. Note: we bind the lob descriptor here */
00037   if (SQLO_SUCCESS != 
00038       (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &k, sizeof(int), NULL, 0)) ||
00039       (sqlo_bind_by_pos(sth, 2, SQLOT_CLOB, &loblp, 0, NULL, 0))
00040       ) {
00041     sqlo_free_lob_desc(dbh, &loblp);
00042     error_exit(dbh, "sqlo_bind_by_pos");
00043   }
00044 
00045   /* execute the statement */
00046   status = sqlo_execute(sth, 1);
00047 
00048   if (SQLO_SUCCESS != status) {
00049     sqlo_free_lob_desc(dbh, &loblp);
00050     error_exit(dbh, "sqlo_execute");
00051   }
00052 
00053   /* write the lob */
00054   status = sqlo_lob_write_buffer(dbh, loblp, MAX_BLOB_BUFFER_DATA, data, 
00055                                  MAX_BLOB_BUFFER_DATA, SQLO_ONE_PIECE);
00056 
00057   if (status < 0) {
00058     sqlo_free_lob_desc(dbh, &loblp);
00059     error_exit(dbh, "sqlo_log_write_buffer");
00060   }
00061 
00062   sqlo_free_lob_desc(dbh, &loblp);
00063   sqlo_close(sth);
00064   
00065   return (1);
00066 }
00067 
00068 void create_blob_table(sqlo_db_handle_t dbh)
00069 {
00070   char * stmt=
00071     "CREATE TABLE T_SQLORA_BLOB (KEY INTEGER, CDATA CLOB, BDATA BLOB)";
00072 
00073   if (SQLO_SUCCESS != sqlo_exec(dbh, stmt))
00074     if (sqlo_geterrcode(dbh) != 955) /* table exists already */
00075       error_exit(dbh, "sqlo_exec");
00076 }
00077 
00078 void drop_blob_table(sqlo_db_handle_t dbh)
00079 {
00080   char * stmt = "DROP TABLE T_SQLORA_BLOB";
00081 
00082   if (SQLO_SUCCESS != sqlo_exec(dbh, stmt))
00083     error_exit(dbh, "sqlo_exec");
00084 }
00085 
00086 void fillbuf(char * data, int len)
00087 {
00088   int i;
00089   for (i = 0; i < len; ++i) {
00090     data[i] = 'A' + i % 26;
00091   }
00092 }
00093 
00094 /* $Id: ex13.c 286 2004-06-15 10:15:52Z kpoitschke $ */

Generated on Mon May 21 13:42:17 2007 for libsqlora8 by  doxygen 1.4.7