You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+58Lines changed: 58 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,64 @@ int main() {
77
77
}
78
78
```
79
79
80
+
Prepared Statements
81
+
=====
82
+
It is possible to retain and reuse statments this will keep the query plan and in case of an complex query or many uses might increase the performance significantly.
83
+
84
+
```c++
85
+
86
+
database db(":memory:");
87
+
88
+
// if you use << on a sqlite::database you get a prepared statment back
89
+
// this will not be executed till it gets destroyed or you execute it explicitly
90
+
auto ps = db << "select ?,? "; // get a prepared parsed and ready statment
91
+
92
+
// first if needed bind values to it
93
+
ps << 5;
94
+
int tmp = 8;
95
+
ps << tmp;
96
+
97
+
// now you can execute it, if the statment was executed once it will not be executed again when it goes out of scope.
98
+
// But beware that it will if it wasn't executed!
99
+
ps >> [&](int a,int b){ ... };
100
+
101
+
// after a successfull execution the statment needs to be reset to be execute again. This will reset the bound values too!
102
+
ps.reset();
103
+
104
+
// If you dont need the returned values you can execute it like this
105
+
ps.execute(); // the statment will not be reset!
106
+
107
+
// there is a convinience operator to execute and reset in one go
108
+
ps++;
109
+
110
+
// To disable the execution of a statment when it goes out of scope and wasn't used
111
+
ps.set_used(true); // or false if you want it to execute even if it was used
112
+
113
+
// Usage Example:
114
+
115
+
auto ps = db << "insert into complex_table_with_lots_of_indices values (?,?,?)";
If you need the handle to the database connection to execute sqlite3 commands directly you can get a managed shared_ptr to it, so it will not close as long as you have a referenc to it.
126
+
127
+
```c++
128
+
sqlite::connection_type con;
129
+
130
+
{ // scope of our db object
131
+
database db(":memory:"); // Make a connection and create in memory db
132
+
con = db.get_sqlite3_connection();
133
+
} // here the temporary db would be lost but we still have the connection for our direct API calls
134
+
135
+
database db2(con); // or we can even make a new database object from it
136
+
```
137
+
80
138
Transactions
81
139
=====
82
140
You can use transactions with `begin;`, `commit;` and `rollback;` commands.
0 commit comments