diff options
Diffstat (limited to 'server/database/database.go')
-rw-r--r-- | server/database/database.go | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/server/database/database.go b/server/database/database.go index 3e60311..16b6f65 100644 --- a/server/database/database.go +++ b/server/database/database.go @@ -6,17 +6,49 @@ * Copyright (c) 2021 Vidhu Kant Sharma */ -// Idk how databases work -// this package is supposed to handle the sqlite database +// Idk how databases work this package is supposed to handle the sqlite database // will figure that out package database import ( - "fmt" + _ "github.com/mattn/go-sqlite3" + "database/sql" ) -// very important function that fucking needed a new package -func SayHello() { - fmt.Println("Hello") +type Item struct { + Model string + Desc string `json:"Description"` + Price int // *float32 + HSN int +} + + +var myDatabase *sql.DB +func init() { + myDatabase, _ = sql.Open("sqlite3", "./openbills.db") +} + +var myItems *sql.Stmt +var addToMyItems *sql.Stmt +func init() { + myItems, _ = myDatabase.Prepare("CREATE TABLE IF NOT EXISTS RegisteredItems (id INTEGER PRIMARY KEY, model TEXT, desc TEXT, price INTEGER, HSN INTEGER)") + myItems.Exec() + + addToMyItems, _ = myDatabase.Prepare("INSERT INTO RegisteredItems (id, model, desc, price, hsn) VALUES (?, ?, ?, ?, ?)") +} + +func GetAllItems() []Item { + var allItems []Item + rows, _ := myDatabase.Query("SELECT model, desc, price, hsn FROM RegisteredItems") + + var model string + var desc string + var price int + var hsn int + for rows.Next() { + rows.Scan(&model, &desc, &price, &hsn) + allItems = append(allItems, Item{model, desc, price, hsn}) + } + return allItems } |