1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| /*
Written by Fabio Ceconello (fabio@iapyx.com.br), 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;
} |
Partager