-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.c
134 lines (115 loc) · 3.7 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "../../libsql.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
libsql_setup((libsql_config_t){0});
libsql_database_t db =
libsql_database_init((libsql_database_desc_t){.path = "local.db"});
if (db.err) {
fprintf(
stderr,
"Error initializing database: %s\n",
libsql_error_message(db.err)
);
return 1;
}
libsql_connection_t conn = libsql_database_connect(db);
if (conn.err) {
fprintf(
stderr,
"Error connecting to database: %s\n",
libsql_error_message(conn.err)
);
return 1;
}
// Drop and create table, insert initial row
const char *setup_sql =
"DROP TABLE IF EXISTS users;"
"CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);"
"INSERT INTO users (name) VALUES ('Iku Turso');";
libsql_batch_t batch = libsql_connection_batch(conn, setup_sql);
if (batch.err) {
fprintf(
stderr,
"Error executing setup batch: %s\n",
libsql_error_message(batch.err)
);
return 1;
}
// Insert combinations of forenames and surnames
const char *forenames[] = {"John", "Mary", "Alice", "Mark"};
const char *surnames[] = {"Doe", "Smith", "Jones", "Taylor"};
int forename_count = sizeof(forenames) / sizeof(forenames[0]);
int surname_count = sizeof(surnames) / sizeof(surnames[0]);
libsql_statement_t stmt =
libsql_connection_prepare(conn, "INSERT INTO users (name) VALUES (?)");
if (stmt.err) {
fprintf(
stderr,
"Error preparing statement: %s\n",
libsql_error_message(stmt.err)
);
return 1;
}
for (int i = 0; i < forename_count; i++) {
for (int j = 0; j < surname_count; j++) {
char full_name[100];
snprintf(
full_name, sizeof(full_name), "%s %s", forenames[i], surnames[j]
);
libsql_statement_reset(stmt);
libsql_statement_bind_value(
stmt, libsql_text(full_name, strlen(full_name))
);
libsql_execute_t result = libsql_statement_execute(stmt);
if (result.err) {
fprintf(
stderr,
"Error inserting %s: %s\n",
full_name,
libsql_error_message(result.err)
);
}
}
}
libsql_statement_deinit(stmt);
// Query and print all users
libsql_statement_t query_stmt =
libsql_connection_prepare(conn, "SELECT * FROM users");
if (query_stmt.err) {
fprintf(
stderr,
"Error preparing query: %s\n",
libsql_error_message(query_stmt.err)
);
return 1;
}
libsql_rows_t rows = libsql_statement_query(query_stmt);
if (rows.err) {
fprintf(
stderr,
"Error executing query: %s\n",
libsql_error_message(rows.err)
);
return 1;
}
libsql_row_t row;
while (!(row = libsql_rows_next(rows)).err && !libsql_row_empty(row)) {
libsql_result_value_t id = libsql_row_value(row, 0);
libsql_result_value_t name = libsql_row_value(row, 1);
if (id.err || name.err) {
fprintf(stderr, "Error retrieving row values\n");
continue;
}
printf(
"%lld %s\n", id.ok.value.integer, (char *)name.ok.value.text.ptr
);
libsql_row_deinit(row);
}
libsql_rows_deinit(rows);
libsql_statement_deinit(query_stmt);
libsql_connection_deinit(conn);
libsql_database_deinit(db);
return 0;
}