-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvadb.c
197 lines (155 loc) · 3.36 KB
/
Provadb.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sqlite3.h>
/*
int callback (void * NotUsed, int argc, char **argv, char **azColName)
{
int i;
printf ("argc = %d\n", argc);
printf("argv0=%s\n", argv[0]);
for(i=0; i<argc; i++){
printf("%d\n", i);
fflush(stdout);
if (strcmp(azColName[i], "one")==0 )
{
printf("%s = %s\n", azColName[i], argv[i]);
}
else printf("%s = %d\n", azColName[i], atoi(argv[i]));
}
printf("\n");
return 0;
}
int main()
{
sqlite3 *db;
char *zErrMsg = 0;
errno=0;
if (sqlite3_open("db/test.db", &db)){
perror("error in sqlite_open");
sqlite3_close(db);
return EXIT_FAILURE;
}
if (sqlite3_exec(db, "select count(*) from tb1", callback, 0, &zErrMsg)){
perror("error in sqlite_exec");
sqlite3_free(zErrMsg);
return EXIT_FAILURE;
}
sqlite3_close(db);
return EXIT_SUCCESS;
}
*/
/*
int callback (void * res, int argc, char **argv, char **azColName)
{
int * i=(int *) res;
long j;
char *endptr;
printf("argv0=%s\n", argv[0]);
errno=0;
j=strtol(argv[0], &endptr, 0);
if (errno!=0)
{
perror("error in strtol");
return EXIT_FAILURE;
}
*i=j;
return EXIT_SUCCESS;
}
int foo()
{
sqlite3 *db;
char *zErrMsg = 0;
int *res;
errno=0;
res = malloc(sizeof(int));
*res=0;
if (sqlite3_open("db/test.db", &db)){
perror("error in sqlite_open");
sqlite3_close(db);
return EXIT_FAILURE;
}
if (sqlite3_exec(db, "select count(*) from tb1", callback, (void *) res, &zErrMsg)){
perror("error in sqlite_exec");
sqlite3_free(zErrMsg);
return EXIT_FAILURE;
}
sqlite3_close(db);
return *res;
}
int main()
{
int result=0;
char string[10];
result =foo();
printf("result=%d\n", result);
snprintf(string, sizeof(char)*10, "hello");
printf("%s\n", string);
return EXIT_SUCCESS;
}
*/
/*
int main()
{
sqlite3 *db;
char *zErrMsg = 0;
char dbcomm[512], text[10];
errno=0;
if (sqlite3_open("db/test.db", &db)){
perror("error in sqlite_open");
sqlite3_close(db);
return EXIT_FAILURE;
}
snprintf(text, sizeof(char)*512, "%s", "image");
printf("%s\n", text);
snprintf(dbcomm, sizeof(char)*512, "INSERT INTO tb2 values('%s', datetime())", text);
printf("%s\n", dbcomm);
errno=0;
if (sqlite3_exec(db, dbcomm, NULL, 0, &zErrMsg)){
perror("error in sqlite_exec");
sqlite3_free(zErrMsg);
return EXIT_FAILURE;
}
sqlite3_close(db);
return EXIT_SUCCESS;
}
* */
int callback (void * res, int argc, char **argv, char **azColName)
{
char *val=(char *) res;
printf("%s\n", argv[0]);
strcpy(val, argv[0]);
printf("%s\n", val);
return EXIT_SUCCESS;
}
int main()
{
sqlite3 *db;
char *zErrMsg = 0;
char *nameimm, dbcomm[512];
nameimm= malloc(sizeof(char)*512);
errno=0;
if (sqlite3_open("db/test.db", &db)){
perror("error in sqlite_open");
sqlite3_close(db);
return EXIT_FAILURE;
}
errno=0;
if (sqlite3_exec(db, "select name from tb2 where date = (select max(date) from tb2)", callback, (void *)nameimm, &zErrMsg)){
perror("error in sqlite_exec");
sqlite3_free(zErrMsg);
return EXIT_FAILURE;
}
printf("%s\n", nameimm);
snprintf(dbcomm, sizeof(char)*512, "DELETE FROM tb2 WHERE name='%s'", nameimm);
errno=0;
if (sqlite3_exec(db, dbcomm, NULL, 0, &zErrMsg)){
perror("error in sqlite_exec");
sqlite3_free(zErrMsg);
return EXIT_FAILURE;
}
sqlite3_close(db);
return EXIT_SUCCESS;
}