Member-only story
Rust language uses SQLite database
SQLite is a widely used lightweight database that hosts data through simple files without complex server configurations. As a result, it has become the preferred database for many desktop and mobile applications. In the Rust ecosystem, the rusqlite
library provides developers with a concise and effective way to manipulate SQLite databases. This article will comprehensively introduce how to use rusqlite
for database operations in Rust.
Start-up installation
Before we start, we need to bring rusqlite
into your Rust project. First, add rusqlite
in the [dependencies]
section of the Cargo.toml
file:
[dependencies]
rusqlite = "0.25.3"
Once the addition is complete, execute cargo build
to download and compile the rusqlite
library and its dependencies.
Open and create a database
To manipulate an SQLite database, you first need to create a connection. Rusqlite
provides a Connection
object to do this. The following example shows how to open an existing database file or create a new one:
use rusqlite::{Connection, Result};fn main() -> Result<()> {
let conn = Connection::open("my_database.db")?; // , `conn`
// ... Ok(())
}
Create table
With the Connection
object, we can start creating the data table:
use rusqlite::{params, Connection, Result};fn main() -> Result<()> {
let conn =…