added basic connection

This commit is contained in:
Nickiel12 2023-12-21 16:44:34 -08:00
parent 0c1b9ae392
commit 51ebc9d841

View file

@ -1,7 +1,7 @@
package main package main
import ( import (
//"database/sql" "database/sql"
"fmt" "fmt"
"log" "log"
"time" "time"
@ -13,12 +13,12 @@ import (
) )
type Transaction struct { type Transaction struct {
id int Id int `db:"trns_id"`
amount decimal.Decimal Amount decimal.Decimal `db:"trns_amount"`
description string Description sql.NullString `db:"trns_description"`
account int Account int `db:"trns_account"`
bucket int Bucket sql.NullInt64 `db:"trns_bucket"`
date time.Time Date time.Time `db:"trns_date"`
} }
func hello(w http.ResponseWriter, req *http.Request) { func hello(w http.ResponseWriter, req *http.Request) {
@ -51,7 +51,7 @@ func main() {
log.Print("Database successfully pinged") log.Print("Database successfully pinged")
transactions := []Transaction{} transactions := []Transaction{}
err = db.Select(&transactions, "SELECT * FROM rcnt.transactions ORDER BY trns_id DESC") err = db.Select(&transactions, "SELECT trns_id, trns_amount, trns_description, trns_account, trns_bucket, trns_date FROM rcnt.transactions ORDER BY trns_id DESC")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -59,7 +59,11 @@ func main() {
log.Print("Select has been run") log.Print("Select has been run")
for _, trns := range transactions { for _, trns := range transactions {
log.Print(fmt.Sprintf("%#v : %#v", trns.amount, trns.id)) if trns.Description.Valid {
log.Print(fmt.Sprintf("%v : %#v", trns.Amount, trns.Id))
} else {
log.Print("amount invalid")
}
} }
log.Print("select all run is done") log.Print("select all run is done")