26. Notes Go language--beedb library use

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

26. Notes Go language--beedb library use

Beedb is a go-to-ORM library that uses go style to manipulate the database, enabling the mapping of a struct to a data table record. is a very lightweight go ORM framework that reduces the complexity of the ORM learning curve and seeks to balance the efficiency and functionality of the ORM as much as possible.

Installation

Support for Go get mode installation, is completely in accordance with the way the go style.

Command line execution: Go get github.com/astaxie/beedb

Requires import of the appropriate database driver package, Database/sql standard interface package, and Beedb package

Import (

"Database/sql"

"Github.com/astaxie/beedb"

_ "Github.com/ziutek/mymysql/godrv"

)

Initialization

DB, err: = SQL. Open ("Mymysql", "test/xiemengjun/123456")

If err! = Nil {

Panic (ERR)

}

ORM: = Beedb. NEW (DB)

Beedb's new function should actually have two parameters, the first parameter is the standard interface of the DB, the second parameter is the database engine used, if you use the database engine is Mysql/sqlite, then the second parameter can be omitted.

If the database you are using is SQL Server, then the initialization requires:

ORM = Beedb. New (DB, "MSSQL")

If you are using PostgreSQL, then the initialization requires:

ORM = Beedb. New (DB, "PG")

Using the previous database table UserInfo, we now establish the corresponding struct

Type Userinfo struct {

Uid int ' PK '//If the table's primary key is not an ID, then you need to add the PK comment, which explicitly says this field is the primary key

Username string

Departname string

Created time. Time

}

Beedb for the hump name will automatically help you to translate into an underscore field, for example, you define the struct name is UserInfo, then to the bottom of the implementation is User_info, field naming also follows the rule.

Inserting data

How to insert a record, we can see that we are manipulating the Strcut object, not the native SQL statement, and finally save the data to the database by calling the Save interface.

var saveone Userinfo

Saveone. Username = "Test AddUser"

Saveone. Departname = "Test adddepartname" Saveone. Created = time. Now ()

Orm. Save (&saveone)

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.