Skip to content

Commit e3c9fdf

Browse files
committed
update readme
1 parent f3ef606 commit e3c9fdf

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

README.md

+33-15
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,22 @@ It is possible to retain and reuse statments this will keep the query plan and i
9393
int tmp = 8;
9494
ps << tmp;
9595

96-
// now you can execute it with `operator>>` or `execute()`.
96+
// now you can execute it with `operator>>` or `execute()`.
9797
// If the statment was executed once it will not be executed again when it goes out of scope.
9898
// But beware that it will execute on destruction if it wasn't executed!
9999
ps >> [&](int a,int b){ ... };
100100

101101
// after a successfull execution the statment needs to be reset to be execute again. This will reset the bound values too!
102102
ps.reset();
103-
103+
104104
// If you dont need the returned values you can execute it like this
105105
ps.execute(); // the statment will not be reset!
106106

107107
// there is a convinience operator to execute and reset in one go
108108
ps++;
109109

110110
// To disable the execution of a statment when it goes out of scope and wasn't used
111-
ps.used(true); // or false if you want it to execute even if it was used
111+
ps.used(true); // or false if you want it to execute even if it was used
112112

113113
// Usage Example:
114114

@@ -131,9 +131,9 @@ Take this example on how to deal with a database backup using SQLITEs own functi
131131
132132
auto con = db.connection(); // get a handle to the DB we want to backup in our scope
133133
// this way we are sure the DB is open and ok while we backup
134-
134+
135135
// Init Backup and make sure its freed on exit or exceptions!
136-
auto state =
136+
auto state =
137137
std::unique_ptr<sqlite3_backup,decltype(&sqlite3_backup_finish)>(
138138
sqlite3_backup_init(backup.connection().get(), "main", con.get(), "main"),
139139
sqlite3_backup_finish
@@ -166,7 +166,7 @@ You can use transactions with `begin;`, `commit;` and `rollback;` commands.
166166
<< u"jack"
167167
<< 68.5;
168168
db << "commit;"; // commit all the changes.
169-
169+
170170
db << "begin;"; // begin another transaction ....
171171
db << "insert into user (age,name,weight) values (?,?,?);" // utf16 string
172172
<< 19
@@ -176,6 +176,24 @@ You can use transactions with `begin;`, `commit;` and `rollback;` commands.
176176

177177
```
178178

179+
Blob
180+
=====
181+
Use `std::vector<T>` to store and retrieve blob data.
182+
`T` could be `char,short,int,long,long long, float or double`.
183+
184+
```c++
185+
db << "CREATE TABLE person (name TEXT, numbers BLOB);";
186+
db << "INSERT INTO person VALUES (?, ?)" << "bob" << vector<int> { 1, 2, 3, 4};
187+
db << "INSERT INTO person VALUES (?, ?)" << "sara" << vector<double> { 1.0, 2.0, 3.0, 4.0};
188+
189+
vector<int> numbers_bob;
190+
db << "SELECT numbers from person where name = ?;" << "bob" >> numbers_bob;
191+
192+
db << "SELECT numbers from person where name = ?;" << "sara" >> [](vector<double> numbers_sara){
193+
for(auto e : numbers_sara) cout << e << ' '; cout << endl;
194+
};
195+
```
196+
179197
Dealing with NULL values
180198
=====
181199
If you have databases where some rows may be null, you can use boost::optional to retain the NULL value between C++ variables and the database. Note that you must enable the boost support by defining _MODERN_SQLITE_BOOST_OPTIONAL_SUPPORT befor importing the header.
@@ -184,45 +202,45 @@ If you have databases where some rows may be null, you can use boost::optional t
184202
185203
#define _MODERN_SQLITE_BOOST_OPTIONAL_SUPPORT
186204
#include <sqlite_modern_cpp.h>
187-
205+
188206
struct User {
189207
long long _id;
190208
boost::optional<int> age;
191209
boost::optional<string> name;
192210
boost::optional<real> weight;
193211
};
194-
212+
195213
{
196214
User user;
197215
user.name = "bob";
198-
216+
199217
// Same database as above
200218
database db("dbfile.db");
201-
219+
202220
// Here, age and weight will be inserted as NULL in the database.
203221
db << "insert into user (age,name,weight) values (?,?,?);"
204222
<< user.age
205223
<< user.name
206224
<< user.weight;
207-
225+
208226
user._id = db.last_insert_rowid();
209227
}
210-
228+
211229
{
212230
// Here, the User instance will retain the NULL value(s) from the database.
213231
db << "select _id,age,name,weight from user where age > ? ;"
214232
<< 18
215233
>> [&](long long id,
216-
boost::optional<int> age,
234+
boost::optional<int> age,
217235
boost::optional<string> name
218236
boost::optional<real> weight) {
219-
237+
220238
User user;
221239
user._id = id;
222240
user.age = age;
223241
user.name = move(name);
224242
user.weight = weight;
225-
243+
226244
cout << "id=" << user._id
227245
<< " age = " << (user.age ? to_string(*user.age) ? string("NULL"))
228246
<< " name = " << (user.name ? *user.name : string("NULL"))

tests/blob_example.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ int main()
4646
cout << "Bad result on line " << __LINE__ << endl;
4747
exit(EXIT_FAILURE);
4848
}
49-
//else { for(auto e : numbers_sara) cout << e << ' '; cout << endl; }
49+
//else {
50+
//db << "SELECT numbers from person where name = ?;" << "sara" >> [](vector<double> numbers_sara){
51+
//for(auto e : numbers_sara) cout << e << ' '; cout << endl;
52+
//};
53+
//}
5054

5155
}
5256
catch(sqlite_exception e)

0 commit comments

Comments
 (0)