When compiling a Rust application that utilizes the rusqlite crate on a Windows environment, the build process may terminate unexpectedly. The compiler output typically indicates a failure during the linking phase with exit code 1181.
The specific error message often reads:
error: linking with `link.exe` failed: exit code: 1181
= note: "LINK : fatal error LNK1181: cannot open input file 'sqlite3.lib'"
This issue arises because the linker cannot locate the SQLite library file required for the build. To resolve this without manually installing SQLite binaries, the rusqlite crate provides a feature flag that leverages the SQLite version built into the Windows operating system.
Configuring Cargo.toml
Modify the project's manifest file to enable the winsqlite3 feature. This directs the dependency to link against the Windows native SQLite library.
[package]
name = "employee_db"
version = "0.1.0"
edition = "2021"
[dependencies]
rusqlite = { version = "0.31.0", features = ["winsqlite3"] }
Implementation Example
The following code demonstrates establishing a connection, creating a schema, inserting a record, and querying data. The example uses an Employee struct to map the database rows.
use rusqlite::{Connection, Result};
#[derive(Debug)]
struct Employee {
employee_id: i32,
full_name: String,
metadata: Option,
}
fn main() -> Result<()> {
// Open a connection to a database file
let db_connection = Connection::open("company.db")?;
// Create the table structure
db_connection.execute(
"CREATE TABLE IF NOT EXISTS employees (
employee_id INTEGER PRIMARY KEY,
full_name TEXT NOT NULL,
metadata BLOB
)",
(),
)?;
let new_hire = Employee {
employee_id: 101,
full_name: "Alice Smith".to_string(),
metadata: None,
};
// Insert data into the table
db_connection.execute(
"INSERT INTO employees (full_name, metadata) VALUES (?1, ?2)",
(&new_hire.full_name, &new_hire.metadata),
)?;
// Query and map results
let mut query = db_connection.prepare("SELECT employee_id, full_name, metadata FROM employees")?;
let employee_results = query.query_map([], |row| {
Ok(Employee {
employee_id: row.get(0)?,
full_name: row.get(1)?,
metadata: row.get(2)?,
})
})?;
for emp in employee_results {
println!("Employee record: {:?}", emp?);
}
Ok(())
}
Alternatively, if cross-platform compatibility is preferred over using system libraries, the bundled feature can be used to compile SQLite from source automatically.