-
Notifications
You must be signed in to change notification settings - Fork 28
/
easySQLite.cpp
153 lines (117 loc) · 3.2 KB
/
easySQLite.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// easySQLite.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "easySQLite/SqlCommon.h"
#include "easySQLite/SqlField.h"
#include "easySQLite/SqlDatabase.h"
#include "easySQLite/SqlTable.h"
#include "UnitTest++/src/UnitTest++.h"
using namespace sql;
void example1()
{
//define table structure
Field definition_tbPerson[] =
{
Field(FIELD_KEY),
Field("fname", type_text, flag_not_null),
Field("lname", type_text, flag_not_null),
Field("birthdate", type_time),
Field(DEFINITION_END),
};
//define database object
sql::Database db;
try
{
//open database file
db.open("test.db");
//define table object
Table tbPerson(db.getHandle(), "person", definition_tbPerson);
//remove table from database if exists
if (tbPerson.exists())
tbPerson.remove();
//create new table
tbPerson.create();
//define new record
Record record(tbPerson.fields());
//set record data
record.setString("fname", "Jan");
record.setString("lname", "Kowalski");
record.setTime("birthdate", time::now());
//add 10 records
for (int index = 0; index < 10; index++)
tbPerson.addRecord(&record);
//select record to update
if (Record* record = tbPerson.getRecordByKeyId(7))
{
record->setString("fname", "Frank");
record->setString("lname", "Sinatra");
record->setNull("birthdate");
tbPerson.updateRecord(record);
}
//load all records
tbPerson.open();
//list loaded records
for (int index = 0; index < tbPerson.recordCount(); index++)
if (Record* record = tbPerson.getRecord(index))
sql::log(record->toString());
sql::log("");
sql::log("ALL OK");
} catch (Exception e) {
printf("ERROR: %s\r\n", e.msg().c_str());
}
}
void example2()
{
Field definition_tbTest[] =
{
Field(FIELD_KEY),
Field("name", type_text, flag_not_null),
Field("valueInt", type_int),
Field("valueDbl", type_float),
Field("valueTxt", type_text),
Field("valueBol", type_bool, flag_not_null),
Field("valueTme", type_time),
Field(DEFINITION_END),
};
Database db;
try
{
db.open("test.db");
Table source(db.getHandle(), "tbTest", definition_tbTest);
if (source.exists())
source.remove();
source.create();
for (int index = 0; index < 5; index++)
{
Record record(source.fields());
record.setString("name", "test_name_1");
record.setInteger("valueInt", 1234567890);
record.setDouble("valueDbl", 12345.67890);
record.setString("valueTxt", "test'value'1");
record.setBool("valueBol", true);
record.setTime("valueTme", time::now());
source.addRecord(&record);
}
if (Table* target = Table::createFromDefinition(db.getHandle(), "_backup_" + source.name(), source.fields()->getDefinition()))
{
if (target->backup(source))
{
sql::log("");
sql::log("ALL OK");
} else {
sql::log(source.errMsg());
sql::log(target->errMsg());
}
delete target;
}
} catch (Exception e) {
printf("ERROR: %s\r\n", e.msg().c_str());
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//example1();
//example2();
sql::log("");
return UnitTest::RunAllTests();
}