Skip to content

Commit 67d00e8

Browse files
committed
Merge pull request #47 from aminroosta/fix46
fix #46
2 parents b606017 + 4833c8f commit 67d00e8

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

hdr/sqlite_modern_cpp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ namespace sqlite {
302302
return *this << std::string(sql);
303303
}
304304

305+
database_binder::chain_type operator<<(const std::u16string& sql) {
306+
return database_binder::chain_type(new database_binder(_db, sql));
307+
}
308+
309+
database_binder::chain_type operator<<(const char16_t* sql) {
310+
return *this << std::u16string(sql);
311+
}
312+
305313
connection_type connection() const { return _db; }
306314

307315
sqlite3_int64 last_insert_rowid() const {

tests/readme_example.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include<iostream>
2+
#include <sqlite_modern_cpp.h>
3+
using namespace sqlite;
4+
using namespace std;
5+
6+
int main() {
7+
8+
try {
9+
// creates a database file 'dbfile.db' if it does not exists.
10+
database db(":memory:");
11+
12+
// executes the query and creates a 'user' table
13+
db <<
14+
"create table if not exists user ("
15+
" _id integer primary key autoincrement not null,"
16+
" age int,"
17+
" name text,"
18+
" weight real"
19+
");";
20+
21+
// inserts a new user record.
22+
// binds the fields to '?' .
23+
// note that only types allowed for bindings are :
24+
// int ,long, long long, float, double
25+
// string , u16string
26+
// sqlite3 only supports utf8 and utf16 strings, you should use std::string for utf8 and std::u16string for utf16.
27+
// note that u"my text" is a utf16 string literal of type char16_t * .
28+
db << "insert into user (age,name,weight) values (?,?,?);"
29+
<< 20
30+
<< u"bob"
31+
<< 83.25;
32+
33+
int age = 21;
34+
float weight = 68.5;
35+
string name = "jack";
36+
db << u"insert into user (age,name,weight) values (?,?,?);" // utf16 query string
37+
<< age
38+
<< name
39+
<< weight;
40+
41+
cout << "The new record got assigned id " << db.last_insert_rowid() << endl;
42+
43+
// slects from user table on a condition ( age > 18 ) and executes
44+
// the lambda for each row returned .
45+
db << "select age,name,weight from user where age > ? ;"
46+
<< 18
47+
>> [&](int age, string name, double weight) {
48+
cout << age << ' ' << name << ' ' << weight << endl;
49+
};
50+
51+
// selects the count(*) from user table
52+
// note that you can extract a single culumn single row result only to : int,long,long,float,double,string,u16string
53+
int count = 0;
54+
db << "select count(*) from user" >> count;
55+
cout << "cout : " << count << endl;
56+
57+
// you can also extract multiple column rows
58+
db << "select age, name from user where _id=1;" >> tie(age, name);
59+
cout << "Age = " << age << ", name = " << name << endl;
60+
61+
// this also works and the returned value will be automatically converted to string
62+
string str_count;
63+
db << "select count(*) from user" >> str_count;
64+
cout << "scount : " << str_count << endl;
65+
}
66+
catch (exception& e) {
67+
cerr << e.what() << endl;
68+
exit(EXIT_FAILURE);
69+
}
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)