forked from openenclave/openenclave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
73 lines (60 loc) · 2.38 KB
/
main.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
// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
static int callback(void* unused, int argc, char** argv, char** column_name)
{
int i;
for (i = 0; i < argc; i++)
{
printf("%s = %s\n", column_name[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
#define SQL_TEST(expr) \
do \
{ \
if ((expr) != SQLITE_OK) \
{ \
fprintf(stderr, "sqlite error: %s\n", sqlite3_errmsg(db)); \
fprintf(stderr, "%s failed\n", #expr); \
abort(); \
} \
else \
{ \
fprintf(stdout, "%s succeeded\n", #expr); \
} \
} while (0)
int main()
{
sqlite3* db = NULL;
const char* sql = NULL;
sqlite3_stmt* stmt = NULL;
// Print version
printf("sqlite version: %s\n", sqlite3_libversion());
// Open database
SQL_TEST(sqlite3_open(":memory:", &db));
// Create table */
sql = "CREATE TABLE PATIENT("
"ID INT PRIMARY KEY NOT NULL,"
"NAME TEXT NOT NULL,"
"AGE INT NOT NULL,"
"ADDRESS TEXT NOT NULL);";
// Execute SQL statement
SQL_TEST(sqlite3_exec(db, sql, callback, 0, NULL));
// Insert items
sql = "INSERT INTO PATIENT (ID,NAME,AGE,ADDRESS) "
"VALUES (1, 'Dennis', 72, 'California'); "
"INSERT INTO PATIENT (ID,NAME,AGE,ADDRESS) "
"VALUES (2, 'Bjarne', 65, 'Texas'); ";
SQL_TEST(sqlite3_exec(db, sql, callback, 0, NULL));
// Fetch items
sql = "SELECT * from PATIENT";
SQL_TEST(sqlite3_exec(db, sql, callback, NULL, NULL));
SQL_TEST(sqlite3_finalize(stmt));
SQL_TEST(sqlite3_close(db));
return 0;
}