The Beego framework supports three database types (mysql,sqlite,postgresql) by default. Although the configuration of these three kinds of databases is very similar, there are different differences. Now it's time to summarize.
The following is the operation of the PostgreSQL database based on the Beego framework.
Models/models.go
1 Package Models2 3 Import (4 "Github.com/astaxie/beego/orm"5 )6 7Type Studentstruct {8 Id Int649NamestringTenAgeint One } A - func init () { -Orm. Registermodel (New(Student)) the}
Main.go
1 Package Main2 3 Import (4 "FMT"5 "Github.com/astaxie/beego"6 "Github.com/astaxie/beego/orm"7 "Postgresql/models"8 9_"GITHUB.COM/LIB/PQ"Ten_"postgresql/routers" One ) A - func init () { - //PostgreSQL Configuration theOrm. Registerdriver ("Postgres"Orm. Dr_postgres)//Registration Driver -Orm. RegisterDatabase ("default","Postgres","user=postgres password=tom dbname=test host=127.0.0.1 port=5432 sslmode=disable") - - /** + * MySQL Configuration - * Registered Driver + * orm. Registerdriver ("MySQL", Orm.) Dr_mysql) A * MySQL User: root, Root secret: Tom, database name: Test, Database alias: Default at * orm. RegisterDatabase ("Default", "MySQL", "Root:[email Protected]/test?charset=utf8") - */ - /** - * Sqlite Configuration - * Registered Driver - * orm. Registerdriver ("SQLite", Orm.) Dr_sqlite) in * Database storage location:./datas/test.db, Database alias: Default - * orm. RegisterDatabase ("Default", "Sqlite3", "./datas/test.db") to */ + //Auto-Build table -Orm. RUNSYNCDB ("default",false,true) the } * $ Func Main () {Panax NotoginsengOrm. Debug =true -o: =ORM. Neworm () theO.using ("default") +Stu: =New(models. Student) AStu. Name ="Tom" theStu. Age = - + - FMT. Println (O.insert (stu)) $ Beego. Run () $}
The Auto-build table and insert database in the PostgreSQL database are as follows:
Beego framework of ORM Module--postgresql