85 lines
1.4 KiB
Go
85 lines
1.4 KiB
Go
package database
|
|
|
|
import (
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
var engine *xorm.Engine
|
|
|
|
type Wol struct {
|
|
ID int64
|
|
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.Error())
|
|
|
|
}
|
|
res, err := engine.IsTableExist("wol")
|
|
if !res {
|
|
CreateTable()
|
|
}
|
|
if err != nil {
|
|
println(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
func CreateTable() int {
|
|
err := engine.Sync2(new(Wol))
|
|
if err != nil {
|
|
println(err.Error())
|
|
}
|
|
return 0
|
|
}
|
|
func Getallwol(everyone *[]Wol) {
|
|
err := engine.Find(&(*everyone))
|
|
if err != nil {
|
|
println("Getallwol err", err.Error())
|
|
}
|
|
}
|
|
|
|
func Getallwoltext() {
|
|
everyone := make([]Wol, 0)
|
|
err := engine.Find(&everyone)
|
|
if err != nil {
|
|
println("Getallwoltext", err.Error())
|
|
}
|
|
println(len(everyone))
|
|
}
|
|
|
|
// 返回插入的数量
|
|
func Insert(wol *Wol) {
|
|
_, err := engine.Insert(wol)
|
|
if err != nil {
|
|
println(err.Error())
|
|
}
|
|
|
|
}
|
|
func Del(id int64) {
|
|
_, err := engine.Where("i_d = ?", id).Delete(&Wol{})
|
|
if err != nil {
|
|
println(err.Error())
|
|
}
|
|
}
|
|
func Edi(wol *Wol, id int64) {
|
|
_, err := engine.Where("i_d = ?", id).Update(wol)
|
|
if err != nil {
|
|
println(err.Error())
|
|
}
|
|
}
|
|
|
|
// 根据id查找
|
|
func GetWolById(id int64, wol *Wol) {
|
|
_, err := engine.Where("i_d = ?", id).Get(wol)
|
|
if err != nil {
|
|
println(err.Error())
|
|
}
|
|
}
|