test the Beego ORM, using the database sqlite3
1 Package Main2 3 Import (4 "FMT"5 "Github.com/astaxie/beego/orm"6_"Github.com/mattn/go-sqlite3"7 )8 9Type articlestruct {TenIdint OneNamestring A } - - func init () { theOrm. Registerdriver ("SQLite", Orm. Dr_sqlite) -Orm. RegisterDatabase ("default","Sqlite3","data.db") -Orm. Registermodel (New(article)) - } + Func Main () { - + /*//CREATE table not successful, then manually build the table A o: = orm. Neworm () at Art: = Article{name: "Sea"} - ///Three return parameters in order: whether the newly created, object ID value, error - if created, id, err: = O.readorcreate (&art, "Name"); err = = Nil { - if created { - FMT. Println ("New Insert an object. ID: ", id) - } else { in FMT. Println ("Get an Object"). ID: ", id) - } to } + */ - the /*//Write Data Success * o: = orm. Neworm () $ Art: = new (article)Panax Notoginseng Art. Name = "Mars" - the FMT. Println (O.insert (art)) + */ A the //query data success but found that the ID will be self-extracting is 0, the actual database is 1 +o: =ORM. Neworm () -Art: = Article{name:"Mars"} $ERR: = O.read (&art,"Name") $ - ifErr = =ORM. errnorows { -Fmt. Println ("query not reached") the}Else ifErr = =ORM. ERRMISSPK { -Fmt. Println ("primary key not found")Wuyi}Else { the FMT. Println (art. Id, art. Name) - } Wu -}
Beego ORM, using the database sqlite3