-
Notifications
You must be signed in to change notification settings - Fork 328
/
example.c
114 lines (102 loc) · 2.68 KB
/
example.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
#include "libsql.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
libsql_connection_t conn;
libsql_rows_t rows;
libsql_row_t row;
libsql_database_t db;
libsql_config config;
const char *err = NULL;
int retval = 0;
char db_path[1024];
char sync = 0;
if (argc > 1) {
char* url = argv[1];
char auth_token[1024];
auth_token[0] = '\0';
if (argc > 2) {
strncpy(auth_token, argv[2], strlen(argv[2]));
}
strncpy(db_path, "test.db", strlen("test.db"));
config.db_path = db_path;
config.primary_url = url;
config.auth_token = auth_token;
config.read_your_writes = 0;
config.encryption_key = NULL;
config.sync_interval = 0;
config.with_webpki = 0;
retval = libsql_open_sync_with_config(config, &db, &err);
if (retval != 0) {
fprintf(stderr, "%s\n", err);
goto quit;
}
sync = 1;
} else {
retval = libsql_open_ext(":memory:", &db, &err);
if (retval != 0) {
fprintf(stderr, "%s\n", err);
goto quit;
}
}
retval = libsql_connect(db, &conn, &err);
if (retval != 0) {
fprintf(stderr, "%s\n", err);
goto quit;
}
retval = libsql_execute(conn, "CREATE TABLE IF NOT EXISTS guest_book_entries (text TEXT)", &err);
if (retval != 0) {
fprintf(stderr, "%s\n", err);
goto quit;
}
retval = libsql_execute(conn, "INSERT INTO guest_book_entries VALUES('hi there')", &err);
if (retval != 0) {
fprintf(stderr, "%s\n", err);
goto quit;
}
retval = libsql_execute(conn, "INSERT INTO guest_book_entries VALUES('some more hi there')", &err);
if (retval != 0) {
fprintf(stderr, "%s\n", err);
goto quit;
}
retval = libsql_query(conn, "SELECT text FROM guest_book_entries", &rows, &err);
if (retval != 0) {
fprintf(stderr, "%s\n", err);
goto quit;
}
while ((retval = libsql_next_row(rows, &row, &err)) == 0) {
if (!err && !row) {
break;
}
const char * value = NULL;
retval = libsql_get_string(row, 0, &value, &err);
if (retval != 0) {
fprintf(stderr, "%s\n", err);
} else {
printf("%s\n", value);
libsql_free_string(value);
value = NULL;
}
err = NULL;
}
if (retval != 0) {
fprintf(stderr, "%s\n", err);
goto quit;
}
if (sync) {
printf("Syncing database to remote...\n");
retval = libsql_sync(db, &err);
if (retval != 0) {
fprintf(stderr, "%s\n", err);
goto quit;
}
printf("Done!\n");
}
quit:
libsql_free_rows(rows);
libsql_disconnect(conn);
libsql_close(db);
return retval;
}