CRUD operations and SQLite3 basics
All SQLite3 operations are going to be done using a library called go-sqlite3
. We can install that package using the following command:
go get github.com/mattn/go-sqlite3
The special thing about this library is that it uses the internal sql
package of Go. We usually import database/sql
and use sql
to execute database queries on the database (here, SQLite3):
import "database/sql"
Now, we can create a database driver and then execute the SQL commands on it using a method called Query
:
sqliteFundamentals.go
:
package main import ( "database/sql" "log" _ "github.com/mattn/go-sqlite3" ) // Book is a placeholder for book type Book struct { id int name string author string } func main() { db, err := sql.Open("sqlite3", "./books.db") log.Println(db) if err != nil { log.Println(err) } // Create table statement, err := db.Prepare("CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY, isbn INTEGER, author VARCHAR...