Comments (8)
For future reference, link to the python documentation:
# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
from sqlitecpp.
Hi, you can simply exec multiple SQL statements separated by semicolon (;)
from sqlitecpp.
Thanks for your quick response. That is what I have been doing in the past but I wanted to see if I could do something more efficiently. I know a lot of other libraries had an executemany that tended to be more efficient. Here's one thing I noticed that I ended up doing. Maybe in the future an execute many can be added that utilizes this.
I ended up creating the prepared statement and reusing the same prepared statement resetting the bindings (I also added the wrapper for clearBindings to clear all existing bindings) and iterating just the bind portion.... Below is the section referencing this technique from the documentation for sqlite.
Thank you again for making this useful library.
3.0 Binding Parameters and Reusing Prepared Statements
In prior discussion, it was assumed that each SQL statement is prepared once, evaluated, then destroyed. However, the SQLite allows the same prepared statement to be evaluated multiple times. This is accomplished using the following routines:
sqlite3_reset()
sqlite3_bind()
After a prepared statement has been evaluated by one or more calls to sqlite3_step(), it can be reset in order to be evaluated again by a call to sqlite3_reset(). Using sqlite3_reset() on an existing prepared statement rather than creating a new prepared statement avoids unnecessary calls to sqlite3_prepare(). In many SQL statements, the time needed to run sqlite3_prepare() equals or exceeds the time needed by sqlite3_step(). So avoiding calls to sqlite3_prepare() can result in a significant performance improvement.
Usually, though, it is not useful to evaluate exactly the same SQL statement more than once. More often, one wants to evaluate similar statements. For example, you might want to evaluate an INSERT statement multiple times though with different values to insert. To accommodate this kind of flexibility, SQLite allows SQL statements to contain parameters which are "bound" to values prior to being evaluated. These values can later be changed and the same prepared statement can be evaluated a second time using the new values.
In SQLite, wherever it is valid to include a string literal, one can use a parameter in one of the following forms:
?
?NNN
:AAA
$AAA
@aaa
In the examples above, NNN is an integer value and AAA is an identifier. A parameter initially has a value of NULL. Prior to calling sqlite3_step() for the first time or immediately after sqlite3_reset(), the application can invoke one of the sqlite3_bind() interfaces to attach values to the parameters. Each call to sqlite3_bind() overrides prior bindings on the same parameter.
An application is allowed to prepare multiple SQL statements in advance and evaluate them as needed. There is no arbitrary limit to the number of outstanding prepared statements.
from sqlitecpp.
Okay, I see what you mean, like the 'executemany()' of the Python binding.
I think this could be handy, but this will be in fact less efficient than doing this by yourself (because you will have to prepare the data into an intermediate dynamically allocated list of arguments).
I reopen this request.
from sqlitecpp.
If you were to make this function, would it be useful to have a bool option to start transaction and end transaction around the execution?
Right now I have a function myself that does this once per second.
from sqlitecpp.
Hi @xsacha, I am not sure of what you mean, can you give a simple example?
from sqlitecpp.
I just mean to have:
db->exec("BEGIN");
query->reset();
for (auto statement: collection)
{
// bind everything in statement
query->bind(..);
}
db->exec("END");
from sqlitecpp.
This as finally been implemented and integrated, thanks to @maxbachmann!
from sqlitecpp.
Related Issues (20)
- Problem with link SqliteCpp with my lib HOT 2
- First time build fails on Windows with MSVC due to carriage return characters HOT 2
- Private members to Protected to enable inheritance
- loop inert and delete, the Active(file) of memory will Abnormal increase
- MinGW cannot find library HOT 2
- Build Error HOT 2
- MSVC: Unresolved symbol SQLite::assertion_failed for example1
- On ORIN: Example1 run error.
- cannot bind to long long (64 bit signed integer)? HOT 4
- it can be shared library? HOT 2
- Issue regarding the overloading of SQLite::Statement::bind
- If the provided filename contains directories or path segments, the database will not be created. HOT 1
- can we add bindBlob(int, const std::string &)? HOT 1
- Is math function possible? HOT 1
- Adding std::optional to bind
- When will the .db-journal file be generated when I don't have any operational database in my project? HOT 1
- LNK2001: unresolved external symbol "SQLite::OPEN_READONLY" HOT 3
- cmake build warning: `cmake --help-policy CMP0148` HOT 1
- c
- (vcpkg) `LNK2019: unresolved external symbol` on Windows 11 for `Database` `std::filesystem::path` constructor HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlitecpp.