test1.c
Go to the documentation of this file.
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sqlite3.h>
5 
6 int myvfs_init(void);
7 
8 static int callback(void * junk __attribute__((unused)), int cnt, char ** vals, char ** col_name)
9 {
10  int i = 0;
11  printf("\n");
12  for (i = 0; i < cnt; ++i) {
13  if (vals[i]) {
14  printf("%s: %s\n", col_name[i], vals[i]);
15  }
16  else {
17  printf("%s: %s\n", col_name[i], "NULL");
18  }
19  }
20  return 0;
21 }
22 
23 int main(int argc, char ** argv)
24 {
25  sqlite3 * db = 0;
26  char * error_msg = 0;
27  int err = 0;
28  if (argc < 2 || argc > 3) {
29  fprintf(stderr, "usage: %s: <db-filename> [<sql-statement>]. If <sql-statement> is omitted take from stdin.\n", argv[0]);
30  exit(1);
31  }
32  myvfs_init();
33  err = sqlite3_open_v2(argv[1], &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, "myvfs");
34  if (err) {
35  fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
36  sqlite3_close(db);
37  exit(1);
38  }
39  if (argc == 3) {
40  err = sqlite3_exec(db, argv[2], callback, 0, &error_msg);
41  }
42  else {
43  char * buf = 0;
44  size_t n = 0;
45  getline(&buf, &n, stdin);
46  err = sqlite3_exec(db, buf, callback, 0, &error_msg);
47  free(buf);
48  }
49  if (err != SQLITE_OK) {
50  fprintf(stderr, "SQL error: %s\n", error_msg);
51  sqlite3_free(error_msg);
52  }
53  sqlite3_close(db);
54  return 0;
55 }
56 
static int callback(void *junk __attribute__((unused)), int cnt, char **vals, char **col_name)
Definition: test1.c:8
int main(int argc, char **argv)
Definition: test1.c:23
int myvfs_init(void)
Definition: myvfs.c:1748