SQLite Go: Connecting to SQLite Databases.

Summary: In this tutorial, you will learn how to connect to the SQLite database using a Go program.

How to connect to an SQLite Database from Go

Step 1. Open your terminal and create a new project directory:

mkdir go-sqlite
cd go-sqliteCode language: Go (go)

Step 2. Initialize a Go module by running the following command from your terminal:

go mod init sqlitetutorial.net/goCode language: Go (go)

This will create a go.mod file. Without doing this step, you will not be able to install any external packages

Step 3. Install the sqlite driver:

go get github.com/glebarez/go-sqliteCode language: Go (go)

The go-sqlite is a pure-Go driver for Go’s native database/sql package.

Step 4. Create a new file main.go that connects to the SQLite database file:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/glebarez/go-sqlite"
)

func main() {
    // Connect to the SQLite database
    db, err := sql.Open("sqlite", "./my.db")
    if err != nil {
        fmt.Println(err)
        return
    }

    defer db.Close()
    fmt.Println("Connected to the SQLite database successfully.")

    // Get the version of SQLite
    var sqliteVersion string
    err = db.QueryRow("select sqlite_version()").Scan(&sqliteVersion)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println(sqliteVersion)
}Code language: Go (go)

How it works.

First, import the database/sql, fmt, and go-sqlite modules:

import (
    "database/sql"
    "fmt"

    _ "github.com/glebarez/go-sqlite"
)Code language: Go (go)

Second, connect to the my.db sqlite database file:

db, err := sql.Open("sqlite", "./my.db")Code language: Go (go)

If the file my.db does not exist in the project directory, the program will create a new one and open a connection. Otherwise, it will create a new connection to the SQLite database file.

The ?_pragma=foreign_keys(1) option enables the foreign key constraint check. Note that SQLite does not enforce the foreign key constraint by default.

If you want to connect to an SQLite database in the memory, you can replace the filename with the literal string":memory:" like this:

db, err := sql.Open("sqlite", ":memory:")Code language: Go (go)

Third, check if an error occurs during the connection process, and display the error message:

if err != nil {
    fmt.Println(err)
    return
}Code language: Go (go)

Fourth, close the database connection before the main function exits i.e., before the program ends:

defer db.Close()Code language: Go (go)

Fifth, display a success message:

fmt.Println("Connected to the SQLite database successfully.")Code language: Go (go)

Finally, get the SQLite version by calling the sqlite_version() function and scan the result into the sqliteVersion variable:

err = db.QueryRow("select sqlite_version()").Scan(&sqliteVersion)Code language: Go (go)

If an error occurs, display the message and return:

if err != nil {
    fmt.Println(err)
    return
}Code language: Go (go)

Otherwise, print the SQLite version:

fmt.Println(sqliteVersion)Code language: Go (go)

It’s important to note that if you call the Open() function to connect to a non-existing SQLite database file and don’t interact with it, the program will not create the SQLite database file.

Summary

  • Install the go-sqlite driver to interact with SQLite databases.
  • Call the Open() function to create a new SQLite database file or open a connection to an existing one.
Was this tutorial helpful ?