xorm/examples/singlemapping.go

58 lines
927 B
Go
Raw Normal View History

package main
import (
2014-01-07 09:33:27 +00:00
"fmt"
2014-01-25 02:31:07 +00:00
"os"
2014-04-11 09:16:43 +00:00
"github.com/go-xorm/xorm"
2014-01-07 09:33:27 +00:00
_ "github.com/mattn/go-sqlite3"
)
2017-01-09 01:52:23 +00:00
// User describes a user
type User struct {
2014-01-07 09:33:27 +00:00
Id int64
Name string
}
2017-01-09 01:53:27 +00:00
// LoginInfo describes a login information
type LoginInfo struct {
2014-01-07 09:33:27 +00:00
Id int64
IP string
UserId int64
// timestamp should be updated by database, so only allow get from db
TimeStamp string `xorm:"<-"`
// assume
Nonuse int `xorm:"->"`
}
func main() {
2014-01-07 09:33:27 +00:00
f := "singleMapping.db"
os.Remove(f)
2017-01-09 01:52:23 +00:00
orm, err := xorm.NewEngine("sqlite3", f)
2014-01-07 09:33:27 +00:00
if err != nil {
fmt.Println(err)
return
}
2017-01-09 01:52:23 +00:00
orm.ShowSQL(true)
err = orm.CreateTables(&User{}, &LoginInfo{})
2014-01-07 09:33:27 +00:00
if err != nil {
fmt.Println(err)
return
}
2017-01-09 01:52:23 +00:00
_, err = orm.Insert(&User{1, "xlw"}, &LoginInfo{1, "127.0.0.1", 1, "", 23})
2014-01-07 09:33:27 +00:00
if err != nil {
fmt.Println(err)
return
}
2014-01-07 09:33:27 +00:00
info := LoginInfo{}
2017-01-09 01:52:23 +00:00
_, err = orm.Id(1).Get(&info)
2014-01-07 09:33:27 +00:00
if err != nil {
fmt.Println(err)
return
}
fmt.Println(info)
}