Skip to content

Commit 9239b75

Browse files
committed
updated readme
1 parent e30408b commit 9239b75

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,43 @@ Use `std::vector<T>` to store and retrieve blob data.
194194
};
195195
```
196196
197-
Dealing with NULL values
197+
NULL values
198198
=====
199-
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.
199+
If you have databases where some rows may be null, you can use `std::unique_ptr<T>` to retain the NULL values between C++ variables and the database.
200200
201201
```c++
202+
db << "CREATE TABLE tbl (id integer,age integer, name string, img blob);";
203+
db << "INSERT INTO tbl VALUES (?, ?, ?, ?);" << 1 << 24 << "bob" << vector<int> { 1, 2 , 3};
204+
unique_ptr<string> ptr_null; // you can even bind empty unique_ptr<T>
205+
db << "INSERT INTO tbl VALUES (?, ?, ?, ?);" << 2 << nullptr << ptr_null << nullptr;
206+
207+
db << "select age,name,img from tbl where id = 1"
208+
>> [](unique_ptr<int> age_p, unique_ptr<string> name_p, unique_ptr<vector<int>> img_p) {
209+
if(age_p == nullptr || name_p == nullptr || img_p == nullptr) {
210+
cerr << "ERROR: values should not be null" << std::endl;
211+
}
212+
213+
cout << "age:" << *age_p << " name:" << *name_p << " img:";
214+
for(auto i : *img_p) cout << i << ","; cout << endl;
215+
};
216+
217+
db << "select age,name,img from tbl where id = 2"
218+
>> [](unique_ptr<int> age_p, unique_ptr<string> name_p, unique_ptr<vector<int>> img_p) {
219+
if(age_p != nullptr || name_p != nullptr || img_p != nullptr) {
220+
cerr << "ERROR: values should be nullptr" << std::endl;
221+
exit(EXIT_FAILURE);
222+
}
223+
224+
cout << "OK all three values are nullptr" << endl;
225+
};
226+
```
227+
228+
NULL values (DEPRICATED)
229+
=====
230+
**Note: this option is deprecated and will be removed in future versions.**
231+
You can enable boost support by defining _MODERN_SQLITE_BOOST_OPTIONAL_SUPPORT before importing sqlite_modern_cpp header.
202232

233+
```c++
203234
#define _MODERN_SQLITE_BOOST_OPTIONAL_SUPPORT
204235
#include <sqlite_modern_cpp.h>
205236

0 commit comments

Comments
 (0)