first commit
This commit is contained in:
commit
b7f9a3e459
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
db.sqlite3
|
||||||
|
dbtest
|
||||||
12
Makefile
Normal file
12
Makefile
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
build: db.sqlite3
|
||||||
|
go build dbtest.go
|
||||||
|
|
||||||
|
run: db.sqlite3
|
||||||
|
go run dbtest.go
|
||||||
|
|
||||||
|
db.sqlite3: migrations/*.sql
|
||||||
|
cat migrations/*.sql | sqlite3 db.sqlite3
|
||||||
|
|
||||||
|
all: build db.sqlite3
|
||||||
|
|
||||||
|
.PHONY: all build run
|
||||||
85
dbtest.go
Normal file
85
dbtest.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
var db *sql.DB
|
||||||
|
|
||||||
|
type Album struct {
|
||||||
|
ID int64
|
||||||
|
Title string
|
||||||
|
Artist string
|
||||||
|
Price float32
|
||||||
|
}
|
||||||
|
type Test_counter struct {
|
||||||
|
ID int64
|
||||||
|
Counter int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var err error
|
||||||
|
db, err = sql.Open("sqlite3", "./db.sqlite3")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
pErr := db.Ping()
|
||||||
|
if pErr != nil {
|
||||||
|
log.Fatal(pErr)
|
||||||
|
}
|
||||||
|
fmt.Println("Connected!")
|
||||||
|
|
||||||
|
count, err := counterIncrement()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Println("You are visitor #", count)
|
||||||
|
|
||||||
|
albums, err := albumsByArtist("John Coltrane")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Albums found: %v\n", albums)
|
||||||
|
}
|
||||||
|
|
||||||
|
func counterIncrement() (int64, error) {
|
||||||
|
_, err := db.Exec("UPDATE `test_counter` SET `counter` = `counter`+1 WHERE `id` = 0")
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("counterIncrement: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
row := db.QueryRow("SELECT counter FROM test_counter WHERE id = 0")
|
||||||
|
if err := row.Scan(&count); err != nil {
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return count, fmt.Errorf("counterIncrement: NO COUNTER")
|
||||||
|
}
|
||||||
|
return count, fmt.Errorf("counterIncrement: %v", err)
|
||||||
|
}
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func albumsByArtist(name string) ([]Album, error) {
|
||||||
|
var albums []Album
|
||||||
|
|
||||||
|
rows, err := db.Query("SELECT * FROM album WHERE artist = ?", name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("albumbsByArtist %q: %v", name, err)
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
for rows.Next() {
|
||||||
|
var alb Album
|
||||||
|
if err := rows.Scan(&alb.ID, &alb.Title, &alb.Artist, &alb.Price); err != nil {
|
||||||
|
return nil, fmt.Errorf("albumbsByArtist %q: %v", name, err)
|
||||||
|
}
|
||||||
|
albums = append(albums, alb)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, fmt.Errorf("albumbsByArtist %q: %v", name, err)
|
||||||
|
}
|
||||||
|
return albums, nil
|
||||||
|
}
|
||||||
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module example/data-access
|
||||||
|
|
||||||
|
go 1.22.3
|
||||||
|
|
||||||
|
require github.com/mattn/go-sqlite3 v1.14.22
|
||||||
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||||
10
migrations/0001_add_table_test_counter.sql
Normal file
10
migrations/0001_add_table_test_counter.sql
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
DROP TABLE IF EXISTS test_counter;
|
||||||
|
CREATE TABLE "test_counter" (
|
||||||
|
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
|
"counter" integer NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO test_counter (
|
||||||
|
id,
|
||||||
|
counter
|
||||||
|
) VALUES ( 0,0 );
|
||||||
8
migrations/0002_tutorial_create_tables.sql
Normal file
8
migrations/0002_tutorial_create_tables.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
DROP TABLE IF EXISTS album;
|
||||||
|
CREATE TABLE album (
|
||||||
|
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
|
title VARCHAR(128) NOT NULL,
|
||||||
|
artist VARCHAR(255) NOT NULL,
|
||||||
|
price DECIMAL(5,2) NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
7
migrations/0003_tutorial_add_data.sql
Normal file
7
migrations/0003_tutorial_add_data.sql
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
INSERT INTO album
|
||||||
|
(title, artist, price)
|
||||||
|
VALUES
|
||||||
|
('Blue Train', 'John Coltrane', 56.99),
|
||||||
|
('Giant Steps', 'John Coltrane', 63.99),
|
||||||
|
('Jeru', 'Gerry Mulligan', 17.99),
|
||||||
|
('Sarah Vaughan', 'Sarah Vaughan', 34.98);
|
||||||
Loading…
x
Reference in New Issue
Block a user