beegoのorm、使用するデータベースsqlite 3

6648 ワード

                            beego orm , sqlite3
1
package main 2 3 import ( 4 "fmt" 5 "github.com/astaxie/beego/orm" 6 _ "github.com/mattn/go-sqlite3" 7 ) 8 9 type Article struct { 10 Id int 11 Name string 12 } 13 14 func init() { 15 orm.RegisterDriver("sqlite", orm.DR_Sqlite) 16 orm.RegisterDataBase("default", "sqlite3", "data.db") 17 orm.RegisterModel(new(Article)) 18 } 19 func main() { 20 21 /* // , 22 o := orm.NewOrm() 23 art := Article{Name: "sea"} 24 // : , Id , 25 if created, id, err := o.ReadOrCreate(&art, "Name"); err == nil { 26 if created { 27 fmt.Println("New Insert an object. Id:", id) 28 } else { 29 fmt.Println("Get an object. Id:", id) 30 } 31 } 32 */ 33 34 /*// 35 o := orm.NewOrm() 36 art := new(Article) 37 art.Name = "Mars" 38 39 fmt.Println(o.Insert(art)) 40 */ 41 42 // id 0, 1 43 o := orm.NewOrm() 44 art := Article{Name: "Mars"} 45 err := o.Read(&art, "Name") 46 47 if err == orm.ErrNoRows { 48 fmt.Println(" ") 49 } else if err == orm.ErrMissPK { 50 fmt.Println(" ") 51 } else { 52 fmt.Println(art.Id, art.Name) 53 } 54 55 }