basic sql connection

This commit is contained in:
Nickiel12 2023-12-19 22:03:09 -08:00
parent 00361743d5
commit 0c1b9ae392
3 changed files with 50 additions and 2 deletions

6
go.mod
View file

@ -2,4 +2,8 @@ module nickiel.net/recount_server
go 1.21.5
require github.com/jmoiron/sqlx v1.3.5 // indirect
require (
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.9
github.com/shopspring/decimal v1.3.1
)

4
go.sum
View file

@ -2,4 +2,8 @@ github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=

View file

@ -1,13 +1,26 @@
package main
import (
//"database/sql"
"fmt"
"log"
"time"
"net/http"
//"github.com/jmoiron/sqlx"
"github.com/shopspring/decimal"
_ "github.com/lib/pq"
"github.com/jmoiron/sqlx"
)
type Transaction struct {
id int
amount decimal.Decimal
description string
account int
bucket int
date time.Time
}
func hello(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "hello\n")
}
@ -24,6 +37,33 @@ func main() {
log.SetPrefix("RecountServer: ")
log.SetFlags(0)
//
db, err := sqlx.Connect("postgres", "user=rcntuser password=Devel@pmentPa$$w0rd host=10.0.0.183 dbname=Borealis sslmode=disable")
if err != nil {
log.Fatal(err)
}
log.Print("Database connected")
// Confirm a successful connection.
if err := db.Ping(); err != nil {
log.Fatal(err)
}
log.Print("Database successfully pinged")
transactions := []Transaction{}
err = db.Select(&transactions, "SELECT * FROM rcnt.transactions ORDER BY trns_id DESC")
if err != nil {
log.Fatal(err)
}
fmt.Print(fmt.Sprintf("%+v\n", transactions))
log.Print("Select has been run")
for _, trns := range transactions {
log.Print(fmt.Sprintf("%#v : %#v", trns.amount, trns.id))
}
log.Print("select all run is done")
http.HandleFunc("/hello", hello)
http.HandleFunc("/headers", headers)