Hello,I just needed to write this little utility to load a regular file to a BLOB in a SQLite database, and I thought it may be useful to someone else, so I'm posting it here.
Best regards, Fabio Ceconello Iapyx
/* Written by Fabio Ceconello ([EMAIL PROTECTED]), 2008-07-18 This is Public Domain as SQLite itself, but please keep this header. */ #include <stdio.h> #include <string.h> #include "sqlite3.h" static const char *help = "Loads a file into a SQLite BLOB field\n\n" "Usage: loadblob <database> <command> <blob>\n" "\t<database> SQLite file name\n" "\t<comando> SQL command, enclosed in double quotes\n" "\t<blob> file to be load to the BLOB\n" "\n\t The command should be an INSERT or UPDATE, and should contain a ? to mark\n" "\t where to put the blob.\n" "\tExample: INSERT INTO test (id, myblob) VALUES (1, ?)\n" "\tThis will insert a record into the 'test' table, and load the file to myblob (obviously, a BLOB column)."; int main(int argc, const char *argv[]) { if (argc < 4) { printf(help); return 1; } char *blob = 0; int size = 0; { FILE *file = fopen(argv[3], "rb"); if (file == 0) { printf("Can't load the BLOB file\n"); return 2; } fseek(file, 0, SEEK_END); size = ftell(file); fseek(file, 0, SEEK_SET); blob = new char[size]; fread(blob, size, 1, file); fclose(file); } sqlite3 *db; if (sqlite3_open(argv[1], &db)) { printf("Can't open the SQLite database\n"); return 3; } const char *sql = argv[2]; sqlite3_stmt *pStmt; if (sqlite3_prepare(db, sql, -1, &pStmt, 0)) { printf("SQL syntax error\n"); return 4; } sqlite3_bind_blob(pStmt, 1, blob, size, SQLITE_STATIC); sqlite3_step(pStmt); sqlite3_finalize(pStmt); sqlite3_close(db); delete blob; return 0; }
_______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users