-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_image.c
73 lines (43 loc) · 1.42 KB
/
read_image.c
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
69
70
71
72
73
#include "sqlite3.h"
#include <stdio.h>
int main(void) {
FILE *fp = fopen("woman2.jpg", "wb");
if (fp == NULL) {
fprintf(stderr, "Cannot open image file\n");
return 1;
}
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("CProjCallingSQLite.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *sql = "SELECT Data FROM Images WHERE Id = 1";
sqlite3_stmt *pStmt;
rc = sqlite3_prepare_v2(db, sql, -1, &pStmt, 0);
if (rc != SQLITE_OK ) {
fprintf(stderr, "Failed to prepare statement\n");
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
rc = sqlite3_step(pStmt);
int bytes = 0;
if (rc == SQLITE_ROW) {
bytes = sqlite3_column_bytes(pStmt, 0);
}
fwrite(sqlite3_column_blob(pStmt, 0), bytes, 1, fp);
if (ferror(fp)) {
fprintf(stderr, "fwrite() failed\n");
return 1;
}
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "Cannot close file handler\n");
}
rc = sqlite3_finalize(pStmt);
sqlite3_close(db);
return 0;
}