From 60f5d0cf22adc77b236f6c5a6e6aa2c7ff39ed77 Mon Sep 17 00:00:00 2001 From: MikunoNaka Date: Wed, 28 Apr 2021 10:12:46 +0530 Subject: implemented a small package to get items from sqlite db --- server/database/database.go | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'server/database/database.go') 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 } -- cgit v1.2.3