xorm/examples/find.go

52 lines
726 B
Go
Raw Normal View History

2016-03-16 15:05:25 +00:00
package main
import (
"fmt"
"os"
"time"
"github.com/go-xorm/xorm"
2018-08-24 08:02:08 +00:00
_ "github.com/mattn/go-sqlite3"
2016-03-16 15:05:25 +00:00
)
2017-01-09 01:52:23 +00:00
// User describes a user
2016-03-16 15:05:25 +00:00
type User struct {
Id int64
Name string
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}
func main() {
f := "conversion.db"
os.Remove(f)
2017-01-09 01:52:23 +00:00
orm, err := xorm.NewEngine("sqlite3", f)
2016-03-16 15:05:25 +00:00
if err != nil {
fmt.Println(err)
return
}
2017-01-09 01:52:23 +00:00
orm.ShowSQL(true)
2016-03-16 15:05:25 +00:00
2017-01-09 01:52:23 +00:00
err = orm.CreateTables(&User{})
2016-03-16 15:05:25 +00:00
if err != nil {
fmt.Println(err)
return
}
2017-01-09 01:52:23 +00:00
_, err = orm.Insert(&User{Id: 1, Name: "xlw"})
2016-03-16 15:05:25 +00:00
if err != nil {
fmt.Println(err)
return
}
users := make([]User, 0)
2017-01-09 01:52:23 +00:00
err = orm.Find(&users)
2016-03-16 15:05:25 +00:00
if err != nil {
fmt.Println(err)
return
}
fmt.Println(users)
}