-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_image.c
122 lines (73 loc) · 2.32 KB
/
insert_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "sqlite3.h"
#include <stdio.h>
int main(int argc, char **argv) {
FILE *fp = fopen("woman.jpg", "rb");
if (fp == NULL) {
fprintf(stderr, "Cannot open image file\n");
return 1;
}
fseek(fp, 0, SEEK_END);
if (ferror(fp)) {
fprintf(stderr, "fseek() failed\n");
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "Cannot close file handler\n");
}
return 1;
}
int flen = ftell(fp);
if (flen == -1) {
perror("error occurred");
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "Cannot close file handler\n");
}
return 1;
}
fseek(fp, 0, SEEK_SET);
if (ferror(fp)) {
fprintf(stderr, "fseek() failed\n");
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "Cannot close file handler\n");
}
return 1;
}
char data[flen+1];
int size = fread(data, 1, flen, fp);
if (ferror(fp)) {
fprintf(stderr, "fread() failed\n");
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "Cannot close file handler\n");
}
return 1;
}
int r = fclose(fp);
if (r == EOF) {
fprintf(stderr, "Cannot close file handler\n");
}
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;
}
sqlite3_stmt *pStmt;
char *sql = "INSERT INTO Images(Data) VALUES(?)";
rc = sqlite3_prepare(db, sql, -1, &pStmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
return 1;
}
sqlite3_bind_blob(pStmt, 1, data, size, SQLITE_STATIC);
rc = sqlite3_step(pStmt);
if (rc != SQLITE_DONE) {
printf("execution failed: %s", sqlite3_errmsg(db));
}
sqlite3_finalize(pStmt);
sqlite3_close(db);
return 0;
}