MySQL orm simple and practical MySQL written with Golang

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

It's okay. With Golang wrote a simple ORM management framework MySQL, of course, simple use a bit, and then everyone submitted a bug, and then I will continue to update and optimize the program

Official website

Introduced

Gomysql is an ORM based on Go-sql-driver, which is a lightweight library. It makes it easy to change database additions and deletions. Of course, test development version, will always be optimized and updated! Keep an eye on us at all times

Installation

go get github.com/go-sql-driver/mysqlgo get github.com/widuu/goinigo get github.com/widuu/gomysql

Dependent packages

Mysql:github.com/go-sql-driver/mysql

Goini:github.com/widuu/goini

Using tutorials

Setting up the configuration file

[database]username = mysql usernamepassword = mysql passwordhostname = mysql hostcharset =  mysql charsetdatabase = database nameport =     mysql port

Initialize configuration

c, _ := gomysql.SetConfig("./conf/conf.ini") //根据配置文件,连接数据库

Querying data

t := c.SetTable("user")                      //设置要处理的表名data := t.FindOne()                          //查询表的一条数据,返回map[int]map[string]string格式gomysql.Print(data)                          //打印数据信息,返回如下的数据格式

data := t.Fileds("id", "password", "username").Where("id =12").FindOne() //Fileds 查询字段,Where where条件FindOne()查询一条//limit sql中的limit OrderBy sql中查询的OrderBydata  = c.SetTable("user").Fileds("id", "password", "username").Where("id>1").Limit(1, 5).OrderBy("id Desc").FindAll() data  = t.FindAll() //查询所有数据,其中OrderBy() Limit() Where() Fileds()等设置条件gomysql.Print(data) //查询的数据都可以用Print()方法友好打印出来信息

Add data

 var value = make(map[string]interface{})  //设置map存储数据,map[key]value value["password"] = "mima3"     value["username"] = "xiaowei" value["id"] = 10 t := c.SetTable("user")                   //设置增加的数据表 t.SetPk("id")                             //设置主键自增的字段名称 i,err := t.Insert(value)                  // 插入数据,返回增加个数和错误信息。返回最后增长的id和错误信息

modifying data

var value = make(map[string]interface{})value["password"] = "mima3"value["username"] = "xiaowei"n, err := c.SetTable("user").Where("id =5").Update(value)  //设置表,条件和修改的内容,返回影响条数和错误信息

Delete data

n, err := c.SetTable("user").Delete("id = 6")   //设置删除的表和数据,返回影响条数和错误信息

Associating operations

INNER JOIN

t := c.SetTable("user")//下面相当于sql语句,打印出是Select user.id,data.keywords,user.username,user.password from user INNER JOIN data ON user.id = data.iddata := t.Fileds("user.id", "data.keywords", "user.username", "user.password").Join("data", "user.id = data.id").FindAll()fmt.Println(data)

Left JOIN

 t.Fileds("user.id", "data.keywords", "user.username", "user.password").LeftJoin("data", "user.id = data.id").FindAll() fmt.Println(data)

Right JOIN

 t.Fileds("user.id", "data.keywords", "user.username", "user.password").RightJoin("data", "user.id = data.id").FindAll() fmt.Println(data)

Full JOIN

 data := t.Fileds("user.id", "data.keywords", "user.username", "user.password").RightJoin("data", "user.id = data.id").FindAll() fmt.Println(data)

Custom SQL statements

// Query()方法 是自定义sql语句的

Insert type of data

n := t.Query("INSERT INTO user (`username`,`password`) VALUES ('xiaoweitest','xiaowei')") //返回最后增长的id

Update|delete

 //update n := c.Query("update user set username='ceshishenma' where id =17 ") fmt.Println(n) //1 返回受影响行数 //delete n := c.Query("delete from user where id=16 ") fmt.Println(n) //1 返回受影响行数

Select

data := c.Query("select username,password from user")fmt.Println(data) //返回map[int]map[string]string 结构的所有数据

Close the database

c.DbClose()     //关闭数据库

Golang MySQL

Without permission, do not reprint this site any article: Micro network» MySQL with Golang written orm simple and practical MySQL

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.