-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbTest.cpp
More file actions
54 lines (51 loc) · 1.64 KB
/
DbTest.cpp
File metadata and controls
54 lines (51 loc) · 1.64 KB
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
#include "DbAccess.h"
#include "DbString.h"
int main()
{
DbAccess db;
// This creates a database file in the current directory if it does not exist.
// SQLite3 must be installed.
printf("Open a database\n");
DbResult result = db.open("DbTest.db");
if(result.isOk())
{
printf("Create a table\n");
DbString command;
command.CREATE_TABLE("Person").COLUMN_DEFS("id INTEGER PRIMARY KEY, name TEXT NOT NULL");
DbStatement statement(db, command.getDbStr().c_str());
result = statement.execute();
}
if(result.isOk())
{
printf("Insert a row\n");
DbStatement statement(db);
DbString insertStr;
insertStr.INSERT_INTO("Person").COLUMNS("name").VALUES(":name");
result = statement.set(insertStr.getDbStr().c_str());
if(result.isOk())
{
statement.bindText(":name", "Fred");
result = statement.execute();
}
}
if(result.isOk())
{
printf("Iterate through all Person rows\n");
DbStatement statement(db);
DbString selectStr;
selectStr.SELECT("id, name").FROM("Person");
result = statement.set(selectStr.getDbStr().c_str());
bool gotRow = true;
while(result.isOk() && gotRow)
{
result = statement.testRow(gotRow);
if(result.isOk() && gotRow)
{
int id = statement.getColumnInt(0);
std::string name = statement.getColumnText(1);
printf(" %d %s\n", id, name.c_str());
}
}
}
return 0;
}