1 #include <stdio.h>
2 
3 #include <zdb.h>
4 
5 /*
6  This example demonstrate most of the functionality of libzdb and can be compiled with a C, OBJ-C(++) or a C++ compiler.
7  Compile: [gcc -std=c99|g++|clang|clang++] -o select select.c -L/<libzdb>/lib -lzdb -I/<libzdb>/include/zdb
8  */
9 
main(void)10 int main(void) {
11         URL_T url = URL_new("sqlite:///tmp/test.db");
12         ConnectionPool_T pool = ConnectionPool_new(url);
13         ConnectionPool_start(pool);
14         Connection_T con = ConnectionPool_getConnection(pool);
15         TRY
16         {
17                 Connection_execute(con, "create table if not exists bleach(name varchar(255), created_at timestamp)");
18                 PreparedStatement_T p = Connection_prepareStatement(con, "insert into bleach values (?, ?)");
19                 const char *bleach[] = {
20                         "Ichigo Kurosaki", "Rukia Kuchiki", "Orihime Inoue",  "Yasutora \"Chad\" Sado",
21                         "Kisuke Urahara", "Ury\u016b Ishida", "Renji Abarai", 0
22                 };
23                 for (time_t i = 0, t = time(0); bleach[i]; i++) {
24                         PreparedStatement_setString(p, 1, bleach[i]);
25                         PreparedStatement_setTimestamp(p, 2, t + i);
26                         PreparedStatement_execute(p);
27                 }
28                 ResultSet_T r = Connection_executeQuery(con,
29                         "select name, datetime(created_at, 'unixepoch', 'localtime') from bleach");
30                 while (ResultSet_next(r))
31                         printf("%-22s\t %s\n", ResultSet_getString(r, 1), ResultSet_getString(r, 2));
32                 Connection_execute(con, "drop table bleach;");
33         }
34         CATCH(SQLException)
35         {
36                 printf("SQLException -- %s\n", Exception_frame.message);
37         }
38         FINALLY
39         {
40                 Connection_close(con);
41         }
42         END_TRY;
43         ConnectionPool_free(&pool);
44         URL_free(&url);
45 }
46