Creating BLOB's in C (IUS 9.14)
Posted in 1999
Topics: General Discussion
Hi, Has anybody an example code in C (datablade), that dumps a structure into a newly created BLOB ? Other code concerning creating BLOB's is welcome. Greetings, Stefan
Example for inserting some binary file to the 'test_table' table in the 'test_db' database. I skip error checking, and I assume, the database is not support loging. For simplify, the table test_table have only one row (with 'binary' type). File name for inserting is specified in the command line as second argument. #include <stdio.h> #include <stdlib.h> #include <string.h> EXEC SQL INCLUDE locator; int connect() { EXEC SQL CONNECT TO 'test_db'; return 0; } int disconnect() { EXEC SQL DISCONNECT CURRENT; return 0; } int insert_data(char* fname) { EXEC SQL BEGIN DECLARE SECTION; struct locator loc; /* we will initialize a locator structure for working with BLOB */ char *dbfname; /* name of the binary file */ EXEC SQL END DECLARE SECTION; dbfname = (char*)malloc(strlen(fname)); strncpy((char*)dbfname, fname, strlen(fname)); loc.loc_type = LOC_FNAME; /* set database engine to operate with files throw their names */ loc.loc_fname = (char*)dbfname; /* file name for inserting */ /* insert the data */ EXEC SQL INSERT INTO test_table VALUES(:loc); return 0; } int main(int argc, char *argv[]) { char *db_fname; int c; if(argc < 3) { fprintf(stderr, "Usage: %s <file name>\\n", argv[0]); return -1; } db_fname = (char*)malloc(strlen(argv[2])); strncpy(db_fname, argv[2], strlen(argv[2])); connect(); insert_data((char*)db_fname); disconnect(); return 0; } Hope, this small example will be usefull for you :-) Ruslan Satlykov.