golang orm

 

package main

import (
    "fmt"
    "github.com/astaxie/beego/orm"
    _"github.com/go-sql-driver/mysql"
)

type User struct{
    Id int
    Name string `orm:"size(100)"`
    Fuck string
    Admin int
}

func init() {
    // set default database
    orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/fuck?charset=utf8", 30)

    // register model
    orm.RegisterModel(new(User))

    // create table
    orm.RunSyncdb("default", false, true)
}
func main() {
    o :=orm.NewOrm()

    user:=User{Name:"slene"}
    id,err :=o.Insert(&user)
    fmt.Printf("ID:%d,ERR:%v\n",id,err)

    user.Name="astaxie"
    num,err:=o.Update(&user)
    fmt.Printf("NUM:%d,ERR:%v\n",num,err)

    // read one
    u := User{Id: user.Id}
    err = o.Read(&u)
    fmt.Printf("ERR: %v\n", err)

    fmt.Println("------------------")
    var maps []orm.Params
    num, err = o.Raw("SELECT * FROM user").Values(&maps)
    for _,term := range maps{
        fmt.Println(term["id"],":",term["name"],term["fuck"])
    }

    //// delete
    //num, err = o.Delete(&u)
    //fmt.Printf("NUM: %d, ERR: %v\n", num, err)
}

 

posted @ 2019-05-27 17:04  landv  阅读(562)  评论(0编辑  收藏  举报