Skip to content

Commit

Permalink
added support for std::utf16string
Browse files Browse the repository at this point in the history
  • Loading branch information
aminroosta committed Mar 24, 2015
1 parent ce69a6c commit e1438f3
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 273 deletions.
48 changes: 25 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sqlite modern cpp wrapper
====

This library is lightweight wrapper around sqlite C api .
This library is a lightweight modern wrapper around sqlite C api .

```c++
#include<iostream>
Expand All @@ -10,60 +10,62 @@ using namespace sqlite;
using namespace std;


int main(){
try {
// creates a database file 'dbfile.db' if not exists
// creates a database file 'dbfile.db' if it does not exists.
database db("dbfile.db");

// executes the query and creates a 'user' table if not exists
// executes the query and creates a 'user' table
db <<
"create table if not exists user ("
" age int,"
" name text,"
" weight real"
");";

// inserts a new user and binds the values to ?
// inserts a new user record.
// binds the fields to '?' .
// note that only types allowed for bindings are :
// int ,long, long long, float, double
// string , wstring
// 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
<< "bob"
<< 83.0;
<< u"bob" // utf16 string
<< 83.25f;

db << "insert into user (age,name,weight) values (?,?,?);"
db << u"insert into user (age,name,weight) values (?,?,?);" // utf16 query string
<< 21
<< L"jack"
<< "jack"
<< 68.5;

// slects from table user on a condition ( age > 18 ) and executes
// the lambda for every row returned .

// 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;
};
cout << age << ' ' << name << ' ' << weight << endl;
};

// selects the count(*) of table user
// note that you can extract a single culumn single row answer only to : int,long,long,float,double,string,wstring
// 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;

// this also works and the returned value will automatically converted to string
string scount;
db << "select count(*) from user" >> scount;
// 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 : " << scount << endl;
}
catch (exception& e){
catch (exception& e) {
cout << e.what() << endl;
}

}
```
*node: for NDK use the full path to you database file : `sqlite::database db("/data/data/com.your.package/dbfile.db")`*.
##License
MIT license - [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php)
59 changes: 0 additions & 59 deletions example.cpp

This file was deleted.

Loading

8 comments on commit e1438f3

@Killili
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did something simular because i had strange problems on Linux where the wstring is 4byte, and on windows its 2byte. I will check if i need to integrated my changes here to.

But hard to see the change when all brackets changed ;)

I appreciate that development on this did not halt 👍!

@aminroosta
Copy link
Collaborator Author

@aminroosta aminroosta commented on e1438f3 Mar 24, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hanito
Copy link

@hanito hanito commented on e1438f3 Mar 24, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Amin. Excuse me I couldn't find another way to ask you this question. I am actually using your library but it is very slow When I am lauching multiple queries in a for loop.

Is there a way to use "transactions" in your library?

@Killili
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is one, i have added a constructor that allows you to create an instance from an existing DB handle.
database(sqlite3* db);

Just open youre own handle and open the transaction on it, then construct a Database instance from that handle and enjoy.

@aminroosta
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use transactions with begin;, commit; and rollback; sql commands.
(don't forget to put all the semicolons at the end of each query).

        db <<
            "create table if not exists user ("
            "   age int,"
            "   name text,"
            "   weight real"
            ");";


        db << "begin;"; // begin a transaction ...   
        db << "insert into user (age,name,weight) values (?,?,?);"
            << 20
            << u"bob"
            << 83.25f;
        db << "insert into user (age,name,weight) values (?,?,?);" // utf16 string
            << 21
            << u"jack"
            << 68.5;
        db << "commit;"; // commit all the changes.

        db << "begin;"; // begin another transaction ....
        db << "insert into user (age,name,weight) values (?,?,?);" // utf16 string
            << 19
            << u"chirs"
            << 82.7;
        db << "rollback;"; // cancel this transaction ...

But i'm not sure transactions are the reason for your comment :it is very slow When I am lauching multiple queries in a for loop.
You can create a new issue in https://github.com/aminroosta/sqlite_modern_cpp/issues page.
If you provide a sample code which explains the problem it will be very helpful.

@Killili
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're right i did that thing with the other constructor so that i would not have to create an instance of the class for a simple one liner like "BEGIN".

And he's right in regards to the performance, at least for querys that require the disk to write stuff, because SQLite will try to sync with the disk every query. On my SSD it takes ~45 seconds for ~1000 "INSERT"s without a transaction and ~1ms with. Because it will then sync only on the "COMMIT".

@hanito
Copy link

@hanito hanito commented on e1438f3 Mar 25, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aminroosta
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update README.md file with this example.
It's likely more programmers are not aware of sqlite transactions.

Please sign in to comment.