Beego framework of ORM Module--postgresql

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.