sqll
Efficient interface to SQLite that doesn't get in your way.
Usage
The two primary methods to interact with an SQLite database through this
crate is through execute and prepare.
The execute function is used for batch statements, and allows for
multiple queries to be specified. prepare only allows for a single
statement to be specified, but in turn permits reading rows and binding
query parameters.
Special consideration needs to be taken about the thread safety of
connections. You can read more about that in the Connection
documentation.
You can find simple examples of this below.
Examples
examples/persons.rs- A simple table with users, a primary key, inserting and querying.examples/axum.rs- Create an in-memory database connection and serve it usingaxum. This showcases how to properly handle external synchronization for the best performance in a real-world scenario.examples/tokio_async.rs- Usingsqllin an asynchronous context withtokio.
Connecting and querying
Here is a simple example of setting up an in-memory connection, creating a table, inserting and querying back some rows:
use ;
let c = open_in_memory?;
c.execute?;
let results = c.prepare?
.
.?;
assert_eq!;
The Row trait.
The Row trait can be used to conveniently read rows from a statement
using next. It can be conveniently implemented using the Row
derive.
use ;
let mut c = open_in_memory?;
c.execute?;
let mut results = c.prepare?;
while let Some = results.?
The Bind trait.
The Bind trait can be used to conveniently bind parameters to
prepared statements, and it can conveniently be implemented for structs
using the Bind derive.
use ;
let c = open_in_memory?;
c.execute?;
let mut stmt = c.prepare?;
stmt.execute?;
stmt.execute?;
let mut query = c.prepare?;
let p = query.?;
assert_eq!;
let p = query.?;
assert_eq!;
Efficient use of prepared Statements
Correct handling of prepared statements are crucial to get good performance out of sqlite. They contain all the state associated with a query and are expensive to construct so they should be re-used.
Using a Prepare::PERSISTENT prepared statement to perform multiple
queries:
use ;
let c = open_in_memory?;
c.execute?;
let mut stmt = c.prepare_with?;
let mut rows = Vecnew;
for age in
let expected = vec!;
assert_eq!;
Use in asynchronous contexts
In order for sqlite to be used in asynchronous contexts, the Statement
object usually needs to be Send and external synchronization necessary.
Since sqlite is a synchronous library we have to defer any work done to a
thread pool such as the one provided by tokio::task::spawn_blocking. To
make a Statement Send you can use Statement::into_send, but using
it is unsafe since correct behavior depends on the build and runtime
configuration of the sqlite library in use.
See the tokio_async example for a complete example.
Features
std- Enable usage of the Rust standard library. Enabled by default.alloc- Enable usage of the Rust alloc library. This is required and is enabled by default. Disabling this option will currently cause a compile error.derive- Add a dependency to and re-export of theRowderive macro.bundled- Use a bundled version of sqlite. The bundle is provided by thesqll-syscrate and the sqlite version used is part of the build metadata of that crate[^sqll-sys].threadsafe- Enable usage of sqlite with the threadsafe option set. We assume any system level libraries have this build option enabled. If this is disabled thebundledfeature has to be enabled. Ifthreadsafeis disabled,ConnectionandStatementdoes not implementSend. But it is also important to understand that if this option is not set, sqlite may not be used by multiple threads at all even if threads have distinct connections. To disable mutexes which allows for efficient one connection per thread theOpenOptions::no_mutexoption should be used instead[^sqll-sys].strict- Enable usage of sqlite with the strict compiler options enabled[^sqll-sys].
[^sqll-sys]: This is a forwarded sqll-sys option, see https://docs.rs/sqll-sys.
License
This is a rewrite of the sqlite crate, and components used from there
have been copied under the MIT license.