Skip to content

Commit 0854375

Browse files
committed
fixed null db result bug
1 parent a1ffdc5 commit 0854375

File tree

2 files changed

+8
-89
lines changed

2 files changed

+8
-89
lines changed

example.cpp

+6-87
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,10 @@
11
#include<iostream>
2+
#include<string>
3+
#include<tuple>
24
#include "sqlite_modern_cpp.h"
35
using namespace sqlite;
46
using namespace std;
57

6-
/*
7-
template <typename T>
8-
struct function_traits
9-
: public function_traits<decltype(&T::operator())>
10-
{};
11-
12-
template <typename ClassType, typename ReturnType, typename... Args>
13-
struct function_traits<ReturnType(ClassType::*)(Args...) const>
14-
// we specialize for pointers to member function
15-
{
16-
enum { arity = sizeof...(Args) };
17-
// arity is the number of arguments.
18-
19-
typedef ReturnType result_type;
20-
21-
template <size_t i>
22-
struct arg
23-
{
24-
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
25-
// the i-th argument is equivalent to the i-th tuple element of a tuple
26-
// composed of those arguments.
27-
};
28-
};
29-
30-
class database_bind {};
31-
32-
template<int N>
33-
class A {
34-
template<typename F>
35-
static void run(F l);
36-
};
37-
38-
template<>
39-
struct A<1> {
40-
template<typename F>
41-
static void run(F l) {
42-
typedef function_traits<decltype(l)> traits;
43-
typedef typename traits::arg<0>::type type_1;
44-
45-
type_1 col_1;
46-
get_from_db(0,col_1);
47-
48-
l(col_1);
49-
}
50-
};
51-
template<>
52-
struct A<2> {
53-
template<typename F>
54-
static void run(F l) {
55-
typedef function_traits<decltype(l)> traits;
56-
typedef typename traits::arg<0>::type type_1;
57-
typedef typename traits::arg<1>::type type_2;
58-
59-
type_1 col_1;
60-
type_2 col_2;
61-
get_from_db(0,col_1);
62-
get_from_db(1,col_2);
63-
64-
l(col_1, col_2);
65-
}
66-
};
67-
68-
69-
void get_from_db(int col_inx, string& str){
70-
// code to get a column from with col_inx from the database
71-
// for simplicity
72-
str = "str_col";
73-
}
74-
void get_from_db(int col_inx, int& i){
75-
// just for simplicity
76-
i = 12;
77-
}
78-
79-
80-
template<typename F>
81-
void operator>>(database_bind dbb, F l)
82-
{
83-
typedef function_traits<decltype(l)> traits;
84-
A<traits::arity>::run(l);
85-
}
86-
87-
*/
888

899
int main(){
9010
try {
@@ -101,7 +21,7 @@ int main(){
10121

10222
// inserts a new user and binds the values to ?
10323
// note that only types allowed for bindings are :
104-
// int ,long, long long, float, double
24+
// int ,long, long long, float, double
10525
// string , wstring
10626
db << "insert into user (age,name,weight) values (?,?,?);"
10727
<< 20
@@ -113,14 +33,14 @@ int main(){
11333
<< L"jack"
11434
<< 68.5;
11535

116-
// slects from table user on a condition ( age > 18 ) and executes
36+
// slects from table user on a condition ( age > 18 ) and executes
11737
// the lambda for every row returned .
11838

11939
db << "select age,name,weight from user where age > ? ;"
12040
<< 18
12141
>> [&](int age, string name, double weight) {
122-
cout << age << ' ' << name << ' ' << weight << endl;
123-
};
42+
cout << age << ' ' << name << ' ' << weight << endl;
43+
};
12444

12545
// selects the count(*) of table user
12646
// note that you can extract a single culumn single row answer only to : int,long,long,float,double,string,wstring
@@ -136,5 +56,4 @@ int main(){
13656
catch (exception& e){
13757
cout << e.what() << endl;
13858
}
139-
14059
}

sqlite_modern_cpp.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ namespace sqlite {
129129
#pragma region get_col_from_db
130130
void get_col_from_db(int inx, int& i){
131131
if (sqlite3_column_type(_stmt, inx) == SQLITE_NULL) i = 0;
132-
i = sqlite3_column_int(_stmt, inx);
132+
else i = sqlite3_column_int(_stmt, inx);
133133
}
134134
void get_col_from_db(int inx, sqlite3_int64& i){
135135
if (sqlite3_column_type(_stmt, inx) == SQLITE_NULL)
136136
i = 0;
137-
i = sqlite3_column_int64(_stmt, inx);
137+
else i = sqlite3_column_int64(_stmt, inx);
138138
}
139139
void get_col_from_db(int inx, string& s){
140140
if (sqlite3_column_type(_stmt, inx) == SQLITE_NULL) s = string();

0 commit comments

Comments
 (0)