go_echo_wol/database/database.go

63 lines
985 B
Go

package database
import (
_ "github.com/mattn/go-sqlite3"
"xorm.io/xorm"
)
var engine *xorm.Engine
type Wol struct {
ID int
Name string `xorm:"varchar(64)"`
Mac string `xorm:"varchar(64)"`
}
func init() {
var err error
file := "./wol.db"
engine, err = xorm.NewEngine("sqlite3", file)
if err != nil {
println(err)
}
}
func CreateTable() int {
err := engine.Sync2(new(Wol))
if err != nil {
println(err)
}
return 0
}
func Getallwol(everyone *[]Wol) {
err := engine.Find(&(*everyone))
if err != nil {
println("Getallwol err", err)
}
}
func Getallwoltext() {
everyone := make([]Wol, 0)
err := engine.Find(&everyone)
if err != nil {
println("Getallwoltext", err)
}
println(len(everyone))
}
func Insert(wol *Wol) {
affected, err := engine.Insert(&wol)
if err != nil {
println(err)
}
println(affected)
}
func Del(id int) {
affected, err := engine.Where("i_d = ?", id).Delete(&Wol{})
if err != nil {
println(err)
}
println(affected, "ok")
}