00001 /* $Id: ex16.c 286 2004-06-15 10:15:52Z kpoitschke $ */ 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 #include "examples.h" 00005 00006 int select_file_from_blob_table(sqlo_db_handle_t dbh, int key, const char *fname ) 00007 { 00008 char * stmt = 00009 "SELECT KEY, BDATA FROM T_SQLORA_BLOB WHERE KEY = :1"; 00010 int status; 00011 sqlo_stmt_handle_t sth = SQLO_STH_INIT; 00012 int k = key; 00013 FILE * fp; 00014 sqlo_lob_desc_t loblp; 00015 unsigned int loblen; 00016 short ind; 00017 00018 if (!(fp = fopen(fname, "w"))) { 00019 printf("ERROR: Cannot open %s for write\n", fname); 00020 return 0; 00021 } 00022 00023 /* parse */ 00024 if (0 > (sth = sqlo_prepare(dbh, stmt))) 00025 error_exit(dbh, "sqlo_prepare"); 00026 00027 /* bind input */ 00028 if (SQLO_SUCCESS != 00029 (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &k, sizeof(k), 0, 0))) 00030 error_exit(dbh, "sqlo_bind_by_pos"); 00031 00032 /* alloc lob desc */ 00033 if (SQLO_SUCCESS != sqlo_alloc_lob_desc(dbh, &loblp)) 00034 error_exit(dbh, "sqlo_alloc_lob_desc"); 00035 00036 /* define output */ 00037 if (SQLO_SUCCESS != 00038 (sqlo_define_by_pos(sth, 1, SQLOT_INT, &k, sizeof(k), 0, 0, 0)) || 00039 (sqlo_define_by_pos(sth, 2, SQLOT_BLOB, &loblp, 0, &ind, 0, 0))) { 00040 sqlo_free_lob_desc(dbh, &loblp); 00041 error_exit(dbh, "sqlo_define_by_pos2"); 00042 } 00043 00044 /* execute */ 00045 status = sqlo_execute(sth, 1); 00046 00047 if ( 0 > status) { 00048 sqlo_free_lob_desc(dbh, &loblp); 00049 error_exit(dbh, "sqlo_execute"); 00050 } 00051 00052 /* get the LOB length */ 00053 status = sqlo_lob_get_length(dbh, loblp, &loblen); 00054 00055 if ( 0 > status) { 00056 sqlo_free_lob_desc(dbh, &loblp); 00057 error_exit(dbh, "sqlo_log_get_length"); 00058 } 00059 00060 /* write it to the file */ 00061 status = sqlo_lob_read_stream(dbh, loblp, loblen, fp); 00062 00063 if ( 0 > status) { 00064 sqlo_free_lob_desc(dbh, &loblp); 00065 sqlo_close(sth); 00066 error_exit(dbh, "sqlo_lob_read_stream"); 00067 } 00068 00069 fclose(fp); 00070 00071 sqlo_free_lob_desc(dbh, &loblp); 00072 sqlo_close(sth); 00073 00074 sqlo_rollback(dbh); 00075 drop_blob_table(dbh); 00076 00077 return (1); 00078 00079 } 00080 00081 /* $Id: ex16.c 286 2004-06-15 10:15:52Z kpoitschke $ */