How to make SQLite work with Diesel ORM for Rust on Windows
Windows is generally difficult to develop on, unless you are using the Windows Subsystem for Linux (WSL). I had to set up SQLite with Diesel ORM for a Rust project on Windows 10 and it was a bit tricky.
Step 1: Install Rust and Microsoft C++ Build Tools
- Rust: Download the installer from https://www.rust-lang.org/tools/install and run it.
- Microsoft C++ Build Tools: follow the steps listed on the Tauri guide.
How to check if Rust is installed:
Run this in Powershell or Command Prompt –rustc --version
. It should output something like rustc 1.76.0 (07dca489a 2024-02-04)
.
How to check if Microsoft C++ Build Tools are installed:
In the Start menu, you should have an app calledDeveloper Command Prompt for VS 2022
. If you don't have it, the Build Tools are not installed. Follow the steps listed on the Tauri guide again.
Step 2: Download and Compile SQLite
-
Go to SQLite’s download page and look for the heading Precompiled Binaries for Windows. Download the zip file for the 64-bit DLL. As of March 2024, you can use this direct link. But it’s better to check the download page for the latest version.
-
In the
C:\
drive, create a new folder calledsqlite64
. Extract the zip file’s contents into that folder. The folder should look like this –
C:\sqlite64\
├── sqlite3.def
├── sqlite3.dll
├── sqlite3.exp
- Next, open the
Developer Command Prompt for VS 2022
from the Start menu. Run the following commands –
cd C:\sqlite64
lib /def:sqlite3.def /out:sqlite3.lib /machine:x64
This should add a new file called sqlite3.lib
to the C:\sqlite64\
folder.
Step 3: Add SQLite to the system PATH
- Type “environment variables” in the Start menu and click on
Edit the system environment variables
. - A System Properties window should open up. Under the
Advanced
tab, click on theEnvironment Variables
button (at the bottom of the window). - Under
User variables
, click onPath
and then click onEdit
. Click onNew
and type –C:\sqlite64
. Press OK. - Under
User variables
, click theNew...
button. In theVariable name
field, typeSQLITE3_LIB_DIR
and in theVariable value
field, typeC:\sqlite64
. Press OK. - Press OK. Press OK again.
Step 4: Add Diesel to the project
Add Diesel to your Rust project by running –
|
|
Fin.
That should do it! I figured this out by parsing the angry comments on this GitHub issue.
Note: If you are coming from a UNIX environment, there is something you should know about sqlite paths on Windows. They look like this:
sqlite:///C:\Users\Username\Documents\database.db
- The beginning should have
sqlite:///
. Yes, that’s THREE slashes. And yes, they are slashes, not backslashes. - The path in your Rust code should have double backslashes:
C:\\Users\\Username\\Documents\\database.db
.