ex14.c

Example for loading a file into a blob column.

00001 /* $Id: ex14.c 286 2004-06-15 10:15:52Z kpoitschke $ */
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include "examples.h"
00005 
00006 int insert_file_into_blob_table(sqlo_db_handle_t dbh, int key, const char * fname )
00007 {
00008 
00009   char * stmt = 
00010     "INSERT INTO T_SQLORA_BLOB (KEY, BDATA) "
00011     "VALUES (:b1, EMPTY_BLOB()) RETURNING BDATA INTO :b2";
00012   sqlo_lob_desc_t loblp;                  /* the lob locator */
00013   sqlo_stmt_handle_t sth;
00014   int status;
00015   FILE * fp;
00016   unsigned filelen;
00017   short ind;
00018   int k = key;
00019 
00020   create_blob_table(dbh);
00021 
00022   if (! (fp = fopen(fname, "r"))) {
00023     printf("ERROR: cannot open file %s\n", fname);
00024     return 0;
00025   }
00026 
00027   /* determine file len */
00028   fseek(fp, 0, SEEK_END);
00029   filelen = ftell(fp);
00030   fseek(fp, 0, 0);
00031 
00032   /* parse */
00033   if (0>(sth = sqlo_prepare(dbh, stmt)))
00034     error_exit(dbh, "sqlo_prepare");
00035 
00036   /* allocate the lob locator */
00037   if (0 > sqlo_alloc_lob_desc(dbh, &loblp))
00038     error_exit(dbh, "sqlo_alloc_lob_desc");
00039 
00040   /* bind input variables */
00041   if (SQLO_SUCCESS != 
00042       (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &k, sizeof(int), NULL, 0)) ||
00043       (sqlo_bind_by_pos(sth, 2, SQLOT_BLOB, &loblp, 0, &ind, 0))
00044       ) {
00045     sqlo_free_lob_desc(dbh, &loblp);
00046     error_exit(dbh, "sqlo_bind_by_pos");
00047   }
00048 
00049   /* execute */
00050   status = sqlo_execute(sth, 1);
00051 
00052   if (SQLO_SUCCESS != status) {
00053     sqlo_free_lob_desc(dbh, &loblp);
00054     error_exit(dbh, "sqlo_execute");
00055   }
00056 
00057   /* write the lob */
00058   status = sqlo_lob_write_stream(dbh, loblp, filelen, fp);
00059 
00060   if (status < 0) {
00061     sqlo_free_lob_desc(dbh, &loblp);
00062     error_exit(dbh, "sqlo_lob_write_stream");
00063   }
00064 
00065   sqlo_close(sth);
00066   sqlo_free_lob_desc(dbh, &loblp);
00067   fclose(fp);
00068 
00069   return (1);
00070 }
00071 
00072 
00073 /* $Id: ex14.c 286 2004-06-15 10:15:52Z kpoitschke $ */

Generated on Mon May 21 13:43:28 2007 for libsqlora8 by  doxygen 1.4.7