test3.cc
Go to the documentation of this file.
2 
3 extern "C" {
4 #include <sqlite3.h>
5 }
6 
7 #include <cstdio>
8 #include <cstdlib>
9 #include <cstring>
10 
11 #ifndef TKEYVFS_NO_ROOT
12 #include "TFile.h"
13 #endif
14 
15 using namespace std;
16 
17 static int
18 callback(void*, int cnt, char** vals, char** col_name)
19 {
20  int i = 0;
21  for (i = 0; i < cnt; ++i) {
22  if (vals[i]) {
23  printf("%s: %s\n", col_name[i], vals[i]);
24  } else {
25  printf("%s: %s\n", col_name[i], "NULL");
26  }
27  }
28  return 0;
29 }
30 
31 int
32 main(int argc, char** argv)
33 {
34  sqlite3* db = 0;
35  char* error_msg = 0;
36  int err = 0;
37  if (argc < 4 || argc > 5) {
38  fprintf(stderr,
39  "usage: %s: <op> <db-filename> <db-tkeyname> [<sql-statement>]\n",
40  argv[0]);
41  exit(1);
42  }
43  tkeyvfs_init();
44 #ifndef TKEYVFS_NO_ROOT
45  TFile* rootFile = 0;
46 #endif
47  if (!strcmp(argv[1], "r")) {
48 #ifndef TKEYVFS_NO_ROOT
49  rootFile = new TFile(argv[2]);
50  err = tkeyvfs_open_v2(argv[3],
51 #else
52  err = tkeyvfs_open_v2(argv[2],
53 #endif
54  &db,
55  SQLITE_OPEN_READONLY
56 #ifndef TKEYVFS_NO_ROOT
57  ,
58  rootFile
59 #endif
60  );
61  } else if (!strcmp(argv[1], "u")) {
62 #ifndef TKEYVFS_NO_ROOT
63  rootFile = new TFile(argv[2], "UPDATE");
64  err = tkeyvfs_open_v2(argv[3],
65 #else
66  err = tkeyvfs_open_v2(argv[2],
67 #endif
68  &db,
69  SQLITE_OPEN_READWRITE
70 #ifndef TKEYVFS_NO_ROOT
71  ,
72  rootFile
73 #endif
74  );
75  } else if (!strcmp(argv[1], "w")) {
76 #ifndef TKEYVFS_NO_ROOT
77  rootFile = new TFile(argv[2], "RECREATE");
78  err = tkeyvfs_open_v2(argv[3],
79 #else
80  err = tkeyvfs_open_v2(argv[2],
81 #endif
82  &db,
83  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
84 #ifndef TKEYVFS_NO_ROOT
85  ,
86  rootFile
87 #endif
88  );
89  } else {
90  fprintf(stderr, "Unrecognized file mode designator %s", argv[1]);
91  exit(1);
92  }
93  if (err) {
94  fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
95  sqlite3_close(db);
96  exit(1);
97  }
98  if (argc == 5) {
99  err = sqlite3_exec(db, argv[4], callback, 0, &error_msg);
100  } else {
101  char* buf = 0;
102  size_t n = 0;
103  getline(&buf, &n, stdin);
104  err = sqlite3_exec(db, buf, callback, 0, &error_msg);
105  free(buf);
106  }
107  if (err != SQLITE_OK) {
108  fprintf(stderr, "SQL error: %s\n", error_msg);
109  sqlite3_free(error_msg);
110  }
111  sqlite3_close(db);
112  return 0;
113 }
STL namespace.
int tkeyvfs_open_v2(const char *filename, sqlite3 **ppDb, int flags, TFile *rootFile )
Definition: tkeyvfs.cc:1837
int tkeyvfs_init(void)
Definition: tkeyvfs.cc:1768
static int callback(void *, int cnt, char **vals, char **col_name)
Definition: test3.cc:18
int main(int argc, char **argv)
Definition: test3.cc:32