Elektromarket/db/db.go

39 lines
1.5 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
}
func DbUpdate(table, column, columnValue, conditionValue string) {
statement, _ := Database.Prepare("UPDATE "+ table + " SET " + column + " = "+ columnValue + " WHERE id = " + conditionValue)
statement.Exec()
}