go_echo_wol/database/database.go

63 lines
985 B
Go
Raw Normal View History

2023-09-02 11:47:53 +08:00
package database
import (
2023-09-04 18:00:09 +08:00
_ "github.com/mattn/go-sqlite3"
2023-09-02 11:47:53 +08:00
"xorm.io/xorm"
)
var engine *xorm.Engine
2023-09-04 18:00:09 +08:00
type Wol struct {
ID int
Name string `xorm:"varchar(64)"`
Mac string `xorm:"varchar(64)"`
}
2023-09-02 16:05:31 +08:00
func init() {
2023-09-02 11:47:53 +08:00
var err error
2023-09-02 16:05:31 +08:00
file := "./wol.db"
2023-09-02 11:47:53 +08:00
engine, err = xorm.NewEngine("sqlite3", file)
if err != nil {
2023-09-05 17:53:38 +08:00
println(err)
2023-09-02 11:47:53 +08:00
}
2023-09-04 18:00:09 +08:00
}
func CreateTable() int {
err := engine.Sync2(new(Wol))
if err != nil {
2023-09-05 17:53:38 +08:00
println(err)
2023-09-04 18:00:09 +08:00
}
return 0
}
func Getallwol(everyone *[]Wol) {
2023-09-05 17:53:38 +08:00
err := engine.Find(&(*everyone))
if err != nil {
println("Getallwol err", err)
}
}
func Getallwoltext() {
everyone := make([]Wol, 0)
2023-09-04 18:00:09 +08:00
err := engine.Find(&everyone)
if err != nil {
2023-09-05 17:53:38 +08:00
println("Getallwoltext", err)
}
println(len(everyone))
}
func Insert(wol *Wol) {
affected, err := engine.Insert(&wol)
if err != nil {
println(err)
2023-09-04 18:00:09 +08:00
}
2023-09-05 17:53:38 +08:00
println(affected)
2023-09-02 11:47:53 +08:00
}
2023-09-08 11:21:13 +08:00
func Del(id int) {
affected, err := engine.Where("i_d = ?", id).Delete(&Wol{})
2023-09-08 10:34:03 +08:00
if err != nil {
println(err)
}
println(affected, "ok")
}