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
+73
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,79 @@ 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
+
database db(":memory:");
86
+
87
+
// if you use << on a sqlite::database you get a prepared statment back
88
+
// this will not be executed till it gets destroyed or you execute it explicitly
89
+
auto ps = db << "select a,b from table where something = ? and anotherthing = ?"; // get a prepared parsed and ready statment
90
+
91
+
// first if needed bind values to it
92
+
ps << 5;
93
+
int tmp = 8;
94
+
ps << tmp;
95
+
96
+
// now you can execute it with `operator>>` or `execute()`.
97
+
// If the statment was executed once it will not be executed again when it goes out of scope.
98
+
// But beware that it will execute on destruction 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.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
+
Take this example on how to deal with a database backup using SQLITEs own functions in a save and modern way.
128
+
```c++
129
+
try {
130
+
database backup("backup"); //Open the database file we want to backup to
131
+
132
+
auto con = db.connection(); // get a handle to the DB we want to backup in our scope
133
+
// this way we are sure the DB is open and ok while we backup
134
+
135
+
// Init Backup and make sure its freed on exit or exceptions!
0 commit comments