beego orm操做mysql數據庫

慢慢弄起來~~mysql

按官方操做文檔試一下。git

那個err重複和user編號問題,之後再弄。。github

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)"`
}

func init() {
	orm.RegisterDataBase("default", "mysql",
		"bg:bgpass@tcp(1.2.3.4:3306)/PLDB?charset=utf8", 30)
	orm.RegisterModel(new(User))
	orm.RunSyncdb("default", false, true)
}

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

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

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

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

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

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

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

  

 

相關文章
相關標籤/搜索