Elektromarket/db/db.go
2018-10-28 11:33:41 +01:00

34 lines
1.2 KiB
Go

package db
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
var Database *sql.DB
func DbConnect() {
Database, _ = sql.Open("sqlite3", "db.db")
}
func DbBuild() {
statement, _ := Database.Prepare("CREATE TABLE IF NOT EXISTS Category (id INTEGER PRIMARY KEY, name TEXT)")
statement.Exec()
statement, _ = Database.Prepare("CREATE TABLE IF NOT EXISTS Product (id INTEGER PRIMARY KEY, categoryId INTEGER, name TEXT, description TEXT, imgUrl TEXT, quantity INTEGER, price INTEGER)")
statement.Exec()
statement, _ = Database.Prepare("CREATE TABLE IF NOT EXISTS PurchasementProduct (id INTEGER PRIMARY KEY, purchasementId INTEGER, productId INTEGER, quantity INTEGER, priceTotal INTEGER)")
statement.Exec()
statement, _ = Database.Prepare("CREATE TABLE IF NOT EXISTS Purchasement (id INTEGER PRIMARY KEY, priceTotal TEXT, firstName TEXT, lastName TEXT, paymentMethod INTEGER, deliveryDate TEXT)")
statement.Exec()
}
func DbInsert(table, columns, values string) {
statement, _ := Database.Prepare("INSERT INTO "+ table +" ("+ columns +") VALUES ("+ values +")")
statement.Exec()
}
func DbSelect(table, columns string) *sql.Rows {
rows, _ := Database.Query("SELECT "+ columns +" FROM "+ table)
return rows
}