-
Notifications
You must be signed in to change notification settings - Fork 3
/
sqlite_example.cpp
577 lines (517 loc) · 26.7 KB
/
sqlite_example.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include "sqlite_driver.hpp"
#include <iomanip>
using namespace std;
using namespace vgi::dbconn::dbi;
using namespace vgi::dbconn::dbd;
constexpr auto DBNAME = ":memory:";
//constexpr auto DBNAME = "DBTEST.db";
int main(int argc, char** argv)
{
/************************
* simple usage
************************/
try
{
cout.precision(12);
cout.setf(ios_base::fixed, ios::floatfield);
cout << endl << "============= Simple example =============" << endl;
cout << "===== connecting to database server\n";
connection conn = driver<sqlite::driver>::load().get_connection(DBNAME);
if (conn.connect())
{
cout << "===== done...\n\n";
statement stmt = conn.get_statement();
cout << "===== drop table if it exists\n";
stmt.execute("drop table if exists test; ");
cout << "===== done...\n\n";
cout << "===== creating table\n";
stmt.execute("create table test ( "
"id integer not null, "
"txt1 text not null, "
"txt2 text null, "
"bool integer not null, "
"flag text not null, "
"short integer not null, "
"long integer not null, "
"float real not null, "
"double real not null, "
"date1 int not null, "
"date2 text not null, "
"date3 real not null, "
"datetime1 int not null, "
"datetime2 text not null, "
"datetime3 real not null, "
"time1 int not null, "
"time2 text not null, "
"time3 real not null, "
"u16str text16 not null, "
"u16char text16 not null, "
"bin blob, "
"primary key(id) ); ");
cout << "===== done...\n\n";
cout << "===== inserting row into the table\n";
stmt.execute("insert into test values ( "
"1, "
"'text1', "
"null, "
"0, "
"'Y', "
"2, "
"167890000, "
"12345.123, "
"122337203685477.58, "
"CURRENT_DATE, "
"CURRENT_DATE, "
"CURRENT_DATE, "
"CURRENT_TIMESTAMP, "
"CURRENT_TIMESTAMP, "
"CURRENT_TIMESTAMP, "
"CURRENT_TIME, "
"CURRENT_TIME, "
"CURRENT_TIME, "
"'\u041F\u0441\u0438\u0445', "
"'\u0414', "
"X'0000008300000000000100000000013c'); ");
cout << "===== done...\n\n";
cout << "===== selecting data from the table\n";
result_set rs = stmt.execute("select * from test;");
int date;
time_t datetime;
double time;
std::u16string u16str;
char16_t char16;
char* c;
std::vector<uint8_t> binvec;
time_t tm;
while (rs.next())
{
size_t i = 0;
cout << "-------------- data by index\n";
cout << rs.column_name(i) << ": >" << rs.get_int(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << rs.get_string(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << (rs.is_null(i) ? "NULL" : rs.get_string(i)) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << rs.get_bool(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << rs.get_char(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << rs.get_short(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << rs.get_long(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << rs.get_float(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << rs.get_double(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << rs.get_date(i) << "<\n"; date = rs.get_date(i); ++i;
cout << rs.column_name(i) << ": >" << rs.get_date(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << rs.get_date(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << ctime(&(tm = rs.get_datetime(i))); ++i;
cout << rs.column_name(i) << ": >" << ctime(&(tm = rs.get_datetime(i))); datetime = rs.get_datetime(i); ++i;
cout << rs.column_name(i) << ": >" << ctime(&(tm = rs.get_datetime(i))); ++i;
cout << rs.column_name(i) << ": >" << rs.get_time(i) << "<\n"; time = rs.get_time(i); ++i;
cout << rs.column_name(i) << ": >" << rs.get_time(i) << "<\n"; ++i;
cout << rs.column_name(i) << ": >" << rs.get_time(i) << "<\n"; ++i;
// Standard code conversion facets
//wstring_convert<codecvt_utf8<char16_t>, char16_t> cv;
//cout << rs.column_name(i) << ": >" << cv.to_bytes(rs.get_u16string(i)) << "<\n"; ++i;
//cout << rs.column_name(i) << ": >" << cv.to_bytes(rs.get_u16char(i)) << "<\n"; ++i;
// Or just print out each char after manual conversion
cout.setf(ios_base::hex, ios::basefield);
cout << rs.column_name(i) << ": >";
u16str = rs.get_u16string(i); ++i;
for (auto chr16 : u16str)
{
// little endian
c = reinterpret_cast<char*>(&chr16);
cout << "\\u" << setfill('0') << setw(2) << hex << uppercase << (int)(*(++c));
cout << setfill('0') << setw(2) << hex << uppercase << (int)(*(--c));
}
cout.setf(ios_base::dec, ios::basefield);
cout << "<\n";
cout << rs.column_name(i) << ": >";
char16 = rs.get_u16char(i); ++i;
c = reinterpret_cast<char*>(&char16);
// little endian
cout << "\\u" << setfill('0') << setw(2) << hex << uppercase << (int)(*(++c));
cout << setfill('0') << setw(2) << hex << uppercase << (int)(*(--c));
cout << "<\n";
// End of code conversion facets
auto bdata1 = rs.get_binary(i);
binvec.insert(binvec.begin(), bdata1.begin(), bdata1.end());
cout << rs.column_name(i) << ": >"; ++i;
for (uint16_t k : binvec)
cout << setfill('0') << setw(2) << k;
cout.setf(ios_base::dec, ios::basefield);
cout << "<\n";
cout << "-------------- data by name\n";
cout << "column id: >" << rs.get_int("id") << "<\n";
cout << "column txt1: >" << rs.get_string("txt1") << "<\n";
cout << "column txt2: >" << (rs.is_null("txt2") ? "NULL" : rs.get_string("txt2")) << "<\n";
cout << "column bool: >" << rs.get_bool("bool") << "<\n";
cout << "column flag: >" << rs.get_char("flag") << "<\n";
cout << "column short: >" << rs.get_long("short") << "<\n";
cout << "column long: >" << rs.get_long("long") << "<\n";
cout << "column float: >" << rs.get_float("float") << "<\n";
cout << "column double: >" << rs.get_double("double") << "<\n";
cout << "column date1: >" << rs.get_date("date1") << "<\n";
cout << "column date2: >" << rs.get_date("date2") << "<\n";
cout << "column date3: >" << rs.get_date("date3") << "<\n";
cout << "column datetime1: >" << ctime(&(tm = rs.get_datetime("datetime1")));
cout << "column datetime2: >" << ctime(&(tm = rs.get_datetime("datetime2")));
cout << "column datetime3: >" << ctime(&(tm = rs.get_datetime("datetime3")));
cout << "column time1: >" << rs.get_time("time1") << "<\n";
cout << "column time2: >" << rs.get_time("time2") << "<\n";
cout << "column time3: >" << rs.get_time("time3") << "<\n";
cout.setf(ios_base::hex, ios::basefield);
cout << "column u16str1: >";
u16str = rs.get_u16string("u16str");
for (auto chr16 : u16str)
{
// little endian
c = reinterpret_cast<char*>(&chr16);
cout << "\\u" << setfill('0') << setw(2) << hex << uppercase << (int)(*(++c));
cout << setfill('0') << setw(2) << hex << uppercase << (int)(*(--c));
}
cout.setf(ios_base::dec, ios::basefield);
cout << "<\n";
cout << "column u16char: >";
char16 = rs.get_u16char("u16char");
c = reinterpret_cast<char*>(&char16);
// little endian
cout << "\\u" << setfill('0') << setw(2) << hex << uppercase << (int)(*(++c));
cout << setfill('0') << setw(2) << hex << uppercase << (int)(*(--c));
cout << "<\n";
auto bdata2 = rs.get_binary("bin");
cout << "column bin: >";
for (uint16_t i : bdata2)
cout << setfill('0') << setw(2) << i;
cout.setf(ios_base::dec, ios::basefield);
cout << "<\n";
}
cout << "===== done...\n\n";
cout << "===== testing prepared statement (stored proc would be the same) data types binding\n";
u16str = u"\u041F\u0441\u0438\u0445";
size_t i = 0;
stmt.prepare("insert into test values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
stmt.set_int(i++, 2);
stmt.set_string(i++, "text1");
stmt.set_null(i++);
stmt.set_bool(i++, false);
stmt.set_char(i++, 'Y');
stmt.set_short(i++, 2);
stmt.set_long(i++, 167890000);
stmt.set_float(i++, 12345.123);
stmt.set_double(i++, 122337203685477.58);
stmt.set_date(i++, date);
stmt.set_date(i++, date);
stmt.set_date(i++, date);
stmt.set_datetime(i++, datetime);
stmt.set_datetime(i++, datetime);
stmt.set_datetime(i++, datetime);
stmt.set_time(i++, time);
stmt.set_time(i++, time);
stmt.set_time(i++, time);
stmt.set_u16string(i++, u16str);
stmt.set_u16char(i++, u16str[0]);
stmt.set_binary(i++, binvec);
stmt.execute();
cout << "===== done...\n\n";
cout << "===== updating row in the table\n";
stmt.execute("update test set txt1 = 'text_1.1' where id = 1; ");
cout << "===== done...\n\n";
cout << "===== deleting from the table\n";
stmt.execute("delete from test where id = 1; ");
cout << "===== done...\n\n";
cout << "===== dropping the table\n";
stmt.execute("drop table test; ");
cout << "===== done...\n\n";
}
else
cout << "===== failed to connect!\n";
}
catch (const exception& e)
{
cout << "===== simple example exception: " << e.what() << endl;
}
/************************
* advanced usage
************************/
try
{
cout << endl << "============= Advanced example =============" << endl;
long version = 0;
string verstr;
bool verbose = true;
cout << "===== connecting to database server using custom settings\n";
/*
* Initialize SQLite driver, get driver information, and set custom properties
*/
sqlite::driver& sqltdriver = driver<sqlite::driver>::load().
// get version
version(version).
version_string(verstr).
// set maximum connections
max_connections(1).
// set custome config settings
config(sqlite::config_flag::MEMSTATUS, 1).
config(sqlite::config_flag::SOFT_HEAP_LIMIT, 8 * 1024 * 1024).
#ifdef SQLITE_CONFIG_LOG
config(sqlite::config_flag::LOG, [](void* data, int errcode, const char* msg)
{
bool isverbose = !!data;
if ((errcode == 0 || errcode == SQLITE_CONSTRAINT || errcode == SQLITE_SCHEMA) && isverbose)
cout << __FUNCTION__ << ": Error Code: " << errcode << ": Message: " << msg << endl;
else
cout << __FUNCTION__ << ": Error Code: " << errcode << ": Message: " << msg << endl;
}, (verbose ? reinterpret_cast<void*>(1) : nullptr))
#endif
config(sqlite::config_flag::MULTITHREAD);
/*
* Print information from the driver
*/
cout << "SQLite Library version number: " << version << endl;
cout << "SQLite Library version string: " << verstr << endl;
/*
* Get connection
*/
cout << "===== connecting to database server\n";
connection conn = sqltdriver.get_connection(DBNAME);
if (conn.connect())
{
cout << "===== done...\n\n";
/*
* Set custom connection properties
*/
static_cast<sqlite::connection&>(conn).
config(sqlite::db_config_flag::LOOKASIDE, nullptr, 0, 0);
statement stmt = conn.get_statement();
cout << "===== drop table if it exists\n";
result_set rs = stmt.execute("drop table if exists test; ");
cout << "rows affected = " << rs.rows_affected() << "\n";
cout << "===== done...\n\n";
cout << "===== creating table\n";
rs = stmt.execute("create table test (id integer not null, txt text null, date int not null, primary key(id) );");
cout << "rows affected = " << rs.rows_affected() << "\n";
cout << "===== done...\n\n";
/********************************************************************
* Use multiple insert statement to populate table.
* Some statements are invalid.
*/
cout << "===== using multiple statements to insert rows into the table - all will fail on invalid table name in one of the statements\n";
// If failed to prepare all statement - then whole chain fails
try
{
rs = stmt.execute("insert into test values (1, 'hello1', CURRENT_DATE); \
insert into test values (1, 'hello1', CURRENT_DATE); \
insert into test values (2, 'hello2', CURRENT_DATE); \
insert into test values (7, 'hello2', CURRENT_DATE); \
insert into bogus values (3, 'hello2', CURRENT_DATE); \
insert into test values (4, 'hello3', CURRENT_DATE); ");
}
catch (const exception& e)
{
cout << e.what() << "\n";
}
cout << "rows affected = " << rs.rows_affected() << "\n";
cout << "===== done...\n\n";
cout << "===== using multiple statements to insert rows into the table - two will fail on broken unique index\n";
// If certain statements fail during runtime - then all the other statements continue to be executed
try
{
rs = stmt.execute("insert into test values (1, 'hello1', CURRENT_DATE); \
insert into test values (1, 'hello1', CURRENT_DATE); \
insert into test values (2, 'hello2', CURRENT_DATE); \
insert into test values (3, 'hello2', CURRENT_DATE); \
insert into test values (4, 'hello3', CURRENT_DATE); \
insert into test values (4, 'hello3', CURRENT_DATE); \
insert into test values (5, 'hello5', CURRENT_DATE); ");
}
catch (const exception& e)
{
cout << e.what() << "\n";
}
cout << "rows affected = " << rs.rows_affected() << "\n";
cout << "===== done...\n\n";
/********************************************************************
* Use multiple update statement to populate table.
* Some statements are invalid.
*/
cout << "===== using multiple statements to update rows in the table - all will fail on invalid table name in one of the statements\n";
// If failed to prepare all statement (invalid table name) - then whole chain fails
try
{
rs = stmt.execute("update test set txt = 'boom' where id = 1; \
update bogus set txt = 'test2' where id = 2; \
update test set id = 3 where id = 4; ");
}
catch (const exception& e)
{
cout << e.what() << "\n";
}
cout << "rows affected = " << rs.rows_affected() << "\n";
cout << "===== done...\n\n";
cout << "===== using multiple statements to update rows in the table - one will fail on invalid id\n";
// If certain statements fail during runtime - then all the other statements continue to be executed
try
{
rs = stmt.execute("update test set txt = 'boom' where id = 5; \
update test set txt = 'test2' where id = 6; ");
}
catch (const exception& e)
{
cout << e.what() << "\n";
}
cout << "rows affected = " << rs.rows_affected() << "\n";
cout << "===== done...\n\n";
/********************************************************************
* Use multiple select statement to retrieve data from table.
* Some statements are invalid.
*/
cout << "===== using multiple statements to select data from the table - all will fail on invalid table name in one of the statements\n";
// If failed to prepare all statement (invalid table name) - then whole chain fails
try
{
rs = stmt.execute( "select id from test where txt = 'hello1'; \
select id, txt from test where txt = 'hello2'; \
select id from test where txt = 'hello4'; \
select id, txt from bogus where txt = 'aa'; \
select id, txt from test where txt = 'hello3'; ");
}
catch (const exception& e)
{
cout << e.what() << "\n";
}
cout << "has data = " << rs.has_data() << "\n";
cout << "more results = " << rs.more_results() << "\n";
cout << "===== done...\n\n";
cout << "===== using multiple statements to select data from the table - one will return no data\n";
// If certain statements fail during runtime - then all the other statements continue to be executed
try
{
rs = stmt.execute( "select id from test where txt = 'hello1'; \
select id, txt from test where txt = 'hello2'; \
select id from test where txt = 'hello4'; \
select id, txt from test where txt = 'hello3'; ");
}
catch (const exception& e)
{
cout << e.what() << "\n";
}
for (int i = 1; rs.more_results(); ++i)
{
try
{
cout << "select " << i << ": data set (" << rs.column_count() << " columns):\n";
if (rs.has_data())
{
while (rs.next())
{
cout << "\trow " << rs.row_count() << ":\n";
cout << "\tcolumn 1: >" << (rs.is_null(0) ? -1 : rs.get_int(0)) << "<\n";
if (rs.column_count() > 1)
cout << "\tcolumn 2: >" << (rs.is_null(1) ? "NULL" : rs.get_string(1)) << "<\n";
}
}
cout << "rows affected = " << rs.rows_affected() << "\n";
cout << "---------\n";
}
catch (const exception& e)
{
cout << "select " << i << ": exception: " << e.what() << "\n";
cout << "---------\n";
}
}
cout << "===== done...\n\n";
/********************************************************************
* Use prepared SQL statements for faster execution of repeated statements
*/
cout << "===== using prepared select statements - repeated execution setting new values\n";
/*
* Prepared SQL statements for select
*/
stmt.prepare("select id, txt from test where id = ? or txt = ?; ");
for (int i = 0; i < 6; ++i)
{
cout << "--------------\n";
stmt.set_int(0, i);
stmt.set_string(1, "hello3");
rs = stmt.execute();
if (rs.has_data())
{
while (rs.next())
{
cout << "\trow " << rs.row_count() << ":\n";
cout << "\t" << rs.column_name(0) << ": >" << (rs.is_null(0) ? -1 : rs.get_int(0)) << "<\n";
cout << "\t" << rs.column_name(1) << ": >" << (rs.is_null(1) ? "NULL" : rs.get_string(1)) << "<\n";
}
cout << "done\n";
}
else
cout << "no data\n";
cout << "rows affected = " << rs.rows_affected() << "\n";
}
cout << "===== done...\n\n";
cout << "===== using prepared update statements - repeated execution setting new values\n";
/*
* Prepared SQL statements for update
*/
stmt.prepare("update test set txt = ?, date = ? where id = ?; ");
for (int i = 4; i < 6; ++i)
{
cout << "--------------\n";
stmt.set_null(0);
stmt.set_long(1, 20170101);
stmt.set_int(2, i);
rs = stmt.execute();
cout << "rows affected = " << rs.rows_affected() << "\n";
}
cout << "===== done...\n\n";
cout << "===== using prepared delete statements - repeated execution setting new values\n";
/*
* Prepared SQL statements for delete
*/
stmt.prepare("delete from test where id = ?;");
cout << "--------------\n";
stmt.set_int(0, 4);
rs = stmt.execute();
cout << "rows affected = " << rs.rows_affected() << "\n";
cout << "===== done...\n\n";
cout << "===== using delete with number of affected rows\n";
/********************************************************************
* Use delete SQL statement.
*/
rs = stmt.execute("delete from test where id = 1 or id = 2;");
cout << "delete : rows affected = " << rs.rows_affected() << "\n";
cout << "===== done...\n\n";
cout << "===== using drop with number of affected rows\n";
/********************************************************************
* Use drop table SQL statement.
*/
rs = stmt.execute("drop table test;");
cout << "drop : rows affected = " << rs.rows_affected() << "\n";
cout << "===== done...\n\n";
cout << "===== using explicit commit\n";
/********************************************************************
* Use autocommit OFF mode
*/
stmt.execute("create table test (id int, txt text NULL, primary key(id) );");
conn.autocommit(false);
cout << "autocommit is OFF\n";
cout << "insert 1 row\n";
stmt.execute("insert into test values (1, 'test1');");
cout << "run commit\n";
conn.commit();
cout << "insert 2 rows\n";
stmt.execute("insert into test values (2, 'test2');");
stmt.execute("insert into test values (3, 'test3');");
cout << "run rollback\n";
conn.rollback();
conn.autocommit(true);
cout << "autocommit is ON\n";
cout << "check row count to make sure rollback worked\n";
rs = stmt.execute( "select count(*) from test;");
rs.next();
cout << "\tTable has >" << rs.get_int(0) << "< rows\n";
cout << "===== done...\n\n";
}
}
catch (const exception& e)
{
cout << "advanced example exception: " << e.what() << endl;
}
return 0;
}