Go & SQLite on Windows
I was in need of a fast in-memory and file-based database for my spark-cli (https://github.com/yaravind/spark-cli) project. I chose SQLite as it is widely used, self-contained and cross-platform. However using it on a Windows PC/Laptop would require little bit of additional set-up. Here I share the step by step instructions of the set-up and a sample usage to get you started.
Set-up
Trying to use the github.com/mattn/go-sqlite3 driver on Windows would give you the following error
go get github.com/mattn/go-sqlite3
# github.com/mattn/go-sqlite3
exec: "gcc": executable file not found in %PATH%
To build an app with go-sqlite3, you nedd gcc
. So follow the steps mentioned below in-order
- Download the GCC from here http://tdm-gcc.tdragon.net/download
- Install GCC
3. Open the MinGW Command Prompt from your Start menu and run the commands as shown below.
Sample Application
Import the necessary packages. You will use the generic sql interface provided by the Go standard sql package database/sql
.
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
Open the connection by specifying the driver name and datasource name. You want an in-memory DB so you specify :memory:
as the datasource as per SQLite convention.
db, err := sql.Open("sqlite3", ":memory:")
checkErr(err)
defer db.Close()
Here is the full application with comments