11sqlite modern cpp wrapper
22====
33
4- This library is lightweight wrapper around sqlite C api .
4+ This library is a lightweight modern wrapper around sqlite C api .
55
66``` c++
77#include < iostream>
@@ -10,60 +10,62 @@ using namespace sqlite;
1010using namespace std ;
1111
1212
13- int main (){
1413 try {
15- // creates a database file 'dbfile.db' if not exists
14+ // creates a database file 'dbfile.db' if it does not exists.
1615 database db("dbfile.db");
1716
18- // executes the query and creates a 'user' table if not exists
17+ // executes the query and creates a 'user' table
1918 db <<
2019 "create table if not exists user ("
2120 " age int,"
2221 " name text,"
2322 " weight real"
2423 ");";
2524
26- // inserts a new user and binds the values to ?
25+ // inserts a new user record.
26+ // binds the fields to '?' .
2727 // 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 * .
3032 db << "insert into user (age,name,weight) values (?,?,?);"
3133 << 20
32- << "bob"
33- << 83.0 ;
34+ << u "bob" // utf16 string
35+ << 83.25f ;
3436
35- db << "insert into user (age,name,weight) values (?,?,?);"
37+ db << u "insert into user (age,name,weight) values (?,?,?);" // utf16 query string
3638 << 21
37- << L "jack"
39+ << "jack"
3840 << 68.5;
3941
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 .
4344 db << "select age,name,weight from user where age > ? ;"
4445 << 18
4546 >> [&](int age, string name, double weight) {
46- cout << age << ' ' << name << ' ' << weight << endl;
47- };
47+ cout << age << ' ' << name << ' ' << weight << endl;
48+ };
4849
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
5152 int count = 0;
5253 db << "select count(*) from user" >> count;
5354 cout << "cout : " << count << endl;
5455
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 ;
5859 cout << "scount : " << scount << endl;
5960 }
60- catch (exception& e){
61+ catch (exception& e) {
6162 cout << e.what() << endl;
6263 }
6364
64- }
6565```
6666
67+ *node: for NDK use the full path to you database file : `sqlite::database db("/data/data/com.your.package/dbfile.db")`*.
68+
6769##License
6870
6971MIT license - [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)
0 commit comments