From 4833c8fd7ee6a90bc42bc86616ca9e9752a01a3f Mon Sep 17 00:00:00 2001 From: amin roosta Date: Sun, 24 Apr 2016 11:33:12 +0430 Subject: [PATCH] fix #46 --- hdr/sqlite_modern_cpp.h | 8 +++++ tests/readme_example.cc | 72 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 tests/readme_example.cc diff --git a/hdr/sqlite_modern_cpp.h b/hdr/sqlite_modern_cpp.h index 66b5a7f2..49ba5b57 100644 --- a/hdr/sqlite_modern_cpp.h +++ b/hdr/sqlite_modern_cpp.h @@ -302,6 +302,14 @@ namespace sqlite { return *this << std::string(sql); } + database_binder::chain_type operator<<(const std::u16string& sql) { + return database_binder::chain_type(new database_binder(_db, sql)); + } + + database_binder::chain_type operator<<(const char16_t* sql) { + return *this << std::u16string(sql); + } + connection_type connection() const { return _db; } sqlite3_int64 last_insert_rowid() const { diff --git a/tests/readme_example.cc b/tests/readme_example.cc new file mode 100644 index 00000000..7a09af11 --- /dev/null +++ b/tests/readme_example.cc @@ -0,0 +1,72 @@ +#include +#include +using namespace sqlite; +using namespace std; + +int main() { + + try { + // creates a database file 'dbfile.db' if it does not exists. + database db(":memory:"); + + // executes the query and creates a 'user' table + db << + "create table if not exists user (" + " _id integer primary key autoincrement not null," + " age int," + " name text," + " weight real" + ");"; + + // inserts a new user record. + // binds the fields to '?' . + // note that only types allowed for bindings are : + // int ,long, long long, float, double + // string , u16string + // sqlite3 only supports utf8 and utf16 strings, you should use std::string for utf8 and std::u16string for utf16. + // note that u"my text" is a utf16 string literal of type char16_t * . + db << "insert into user (age,name,weight) values (?,?,?);" + << 20 + << u"bob" + << 83.25; + + int age = 21; + float weight = 68.5; + string name = "jack"; + db << u"insert into user (age,name,weight) values (?,?,?);" // utf16 query string + << age + << name + << weight; + + cout << "The new record got assigned id " << db.last_insert_rowid() << endl; + + // slects from user table on a condition ( age > 18 ) and executes + // the lambda for each row returned . + db << "select age,name,weight from user where age > ? ;" + << 18 + >> [&](int age, string name, double weight) { + cout << age << ' ' << name << ' ' << weight << endl; + }; + + // selects the count(*) from user table + // note that you can extract a single culumn single row result only to : int,long,long,float,double,string,u16string + int count = 0; + db << "select count(*) from user" >> count; + cout << "cout : " << count << endl; + + // you can also extract multiple column rows + db << "select age, name from user where _id=1;" >> tie(age, name); + cout << "Age = " << age << ", name = " << name << endl; + + // this also works and the returned value will be automatically converted to string + string str_count; + db << "select count(*) from user" >> str_count; + cout << "scount : " << str_count << endl; + } + catch (exception& e) { + cerr << e.what() << endl; + exit(EXIT_FAILURE); + } + + return 0; +}