Golang – Fetching Data from Sqlite Database

Before you can use this example, you need to install go-sqlite3.

Here is how to select rows from a Sqlite database with Go, the programming language of google.

package main

import (    
    "fmt"        
    "database/sql"    
    _ "github.com/mattn/go-sqlite3"
)

func postScrape() {
    
    db, err := sql.Open("sqlite3", "mysqlitedatabase.db")
    checkErr(err)
        
    rows, err := db.Query("SELECT * FROM users")
    checkErr(err)
    
    var uid int
    var name string

    for rows.Next() {
        err = rows.Scan(&uid, &name)
        checkErr(err)
        fmt.Println(uid)
        fmt.Println(name)

    }
    
    defer rows.Close()
    
    db.Close()
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}
    
func main() {
    postScrape()
}

Leave a Comment

Your email address will not be published.