1
1
sqlite modern cpp wrapper
2
2
====
3
3
4
- This library is lightweight wrapper around sqlite C api .
4
+ This library is a lightweight modern wrapper around sqlite C api .
5
5
6
6
``` c++
7
7
#include < iostream>
@@ -10,60 +10,62 @@ using namespace sqlite;
10
10
using namespace std ;
11
11
12
12
13
- int main (){
14
13
try {
15
- // creates a database file 'dbfile.db' if not exists
14
+ // creates a database file 'dbfile.db' if it does not exists.
16
15
database db("dbfile.db");
17
16
18
- // executes the query and creates a 'user' table if not exists
17
+ // executes the query and creates a 'user' table
19
18
db <<
20
19
"create table if not exists user ("
21
20
" age int,"
22
21
" name text,"
23
22
" weight real"
24
23
");";
25
24
26
- // inserts a new user and binds the values to ?
25
+ // inserts a new user record.
26
+ // binds the fields to '?' .
27
27
// note that only types allowed for bindings are :
28
- // int ,long, long long, float, double
29
- // string , wstring
28
+ // int ,long, long long, float, double
29
+ // string , u16string
30
+ // sqlite3 only supports utf8 and utf16 strings, you should use std::string for utf8 and std::u16string for utf16.
31
+ // note that u"my text" is a utf16 string literal of type char16_t * .
30
32
db << "insert into user (age,name,weight) values (?,?,?);"
31
33
<< 20
32
- << "bob"
33
- << 83.0 ;
34
+ << u "bob" // utf16 string
35
+ << 83.25f ;
34
36
35
- db << "insert into user (age,name,weight) values (?,?,?);"
37
+ db << u "insert into user (age,name,weight) values (?,?,?);" // utf16 query string
36
38
<< 21
37
- << L "jack"
39
+ << "jack"
38
40
<< 68.5;
39
41
40
- // slects from table user on a condition ( age > 18 ) and executes
41
- // the lambda for every row returned .
42
-
42
+ // slects from user table on a condition ( age > 18 ) and executes
43
+ // the lambda for each row returned .
43
44
db << "select age,name,weight from user where age > ? ;"
44
45
<< 18
45
46
>> [&](int age, string name, double weight) {
46
- cout << age << ' ' << name << ' ' << weight << endl;
47
- };
47
+ cout << age << ' ' << name << ' ' << weight << endl;
48
+ };
48
49
49
- // selects the count(*) of table user
50
- // note that you can extract a single culumn single row answer only to : int,long,long,float,double,string,wstring
50
+ // selects the count(*) from user table
51
+ // note that you can extract a single culumn single row result only to : int,long,long,float,double,string,u16string
51
52
int count = 0;
52
53
db << "select count(*) from user" >> count;
53
54
cout << "cout : " << count << endl;
54
55
55
- // this also works and the returned value will automatically converted to string
56
- string scount ;
57
- db << "select count(*) from user" >> scount ;
56
+ // this also works and the returned value will be automatically converted to string
57
+ string str_count ;
58
+ db << "select count(*) from user" >> str_count ;
58
59
cout << "scount : " << scount << endl;
59
60
}
60
- catch (exception& e){
61
+ catch (exception& e) {
61
62
cout << e.what() << endl;
62
63
}
63
64
64
- }
65
65
```
66
66
67
+ *node: for NDK use the full path to you database file : `sqlite::database db("/data/data/com.your.package/dbfile.db")`*.
68
+
67
69
##License
68
70
69
71
MIT license - [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)
0 commit comments