这期内容当中小编将会给大家带来有关GO语言中怎么实现Mysql数据库的CURD操作,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
package main
import (
"fmt"
//并不需要使用其API,只需要执行该包的init方法(加载MySQL是驱动程序)
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
此处需要导入导入mysql驱动包和增强版Mysql操作库Sqlx。
如果不清楚如何导入第三方包,请查看我的技术博客:手把手教你怎么使用Go语言第三方库。
//执行insert操作
func main() {
//连接数据库
//driverName:mysql,表示驱动器的名称是mysql也就上面"github.com/go-sql-driver/mysql"导入的驱动器。
//dataSourceName:root:123456@tcp(localhost:3306)/mydb 账户名:密码@tcp(ip:端口)/数据库名称
//sqlx.Open返回一个*sqlx.DB和错误。
db, _ := sqlx.Open("mysql", "root:123456@tcp(localhost:3306)/mydb")
defer db.Close()
//执行增删改
//query里面是sql语句。
result, e := db.Exec("insert into person(name,age,rmb,gender,brithday) values(?,?,?,?,?);", "小扬", 21, 8888, true, 20000101)
if e!=nil{
fmt.Println("err=",e)
return
}
// RowsAffected returns the number of rows affected by an
// update, insert, or delete. Not every database or database
// driver may support this.
rowsAffected, _ := result.RowsAffected()
// LastInsertId returns the integer generated by the database
// in response to a command. Typically this will be from an
// "auto increment" column when inserting a new row. Not all
// databases support this feature, and the syntax of such
// statements varies.
lastInsertId, _ := result.LastInsertId()
fmt.Println("受影响的行数=",rowsAffected)
fmt.Println("最后一行的ID=",lastInsertId)
}
使用sqlx包的Open连接数据库。
driverName:mysql,表示驱动器的名称是mysql也就上面"github.com/go-sql-driver/mysql"导入的驱动器。
dataSourceName是root:123456@tcp(localhost:3306)/mydb 它的含义是 账户名:密码@tcp(ip:端口)/数据库名称。
sqlx.Open返回一个*sqlx.DB和错误。
然后执行db.Exec()操作。
result, e := db.Exec("insert into person(name,age,rmb,gender,brithday) values(?,?,?,?,?);", "小扬", 21, 8888, true, 20000101)
第一个参数是query语句。
rowsAffected, _ := result.RowsAffected()
lastInsertId, _ := result.LastInsertId()
RowsAffected()求受影响的行数。RowsAffected返回update, insert, or delete影响的行数。不是每一个数据库和数据库驱动可能支持这个。
LastInsertId()求插入的最后一行的ID。
LastInsertId返回数据库生成的最后一个ID。通常,这来自插入新行时的“自动递增”列。不是所有数据库都支持此功能。
result, e := db.Exec("delete from person where name not like ?;", "%扬")
还是执行db.Exec(),第一个参数是delete语句。
查看该操作是否执行成功。
成功!!!试一试吧!
result, e := db.Exec("update person set name = ? where id = ?;", "大扬", 1)
成功执行!
来看一看结果吧!
现在可以看到数据更新成功。将id为1的数据的name项更新为”大扬“。
这里两个?,后面就要有两个参数。
package main
import (
"fmt"
//并不需要使用其API,只需要执行该包的init方法(加载MySQL是驱动程序)
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Person struct {
// 对应name表字段
Name string `db:"name"`
// 对应age表字段
Age int `db:"age"`
// 对应rmb表字段
Money float64 `db:"rmb"`
}
func main() {
db, _ := sqlx.Open("mysql", "root:123456@tcp(localhost:3306)/mydb")
defer db.Close()
//预定义Person切片用于接收查询结果
var ps []Person
//执行查询,得到Perosn对象的集合,丢入预定义的ps地址
e := db.Select(&ps, "select name,age,rmb from person where name like ?;", "%扬")
if e != nil{
fmt.Println("err=",e)
}
fmt.Println("查询成功",ps)
}
Person结构体里面的属性对应数据库里面的字段。比如:
Age int `db:"age"`
表示Age对应表里面的字段age。
type Person struct {
// 对应name表字段
Name string `db:"name"`
// 对应age表字段
Age int `db:"age"`
// 对应rmb表字段
Money float64 `db:"rmb"`
}
var ps []Person
因为查询的结果可能为多条,所以使用Person切片。然后将查询结果放入ps中
提示:要使用ps的指针!
e := db.Select(&ps, "select name,age,rmb from person where name like ?;", "%扬")
下面我们来看看查询结果:
上述就是小编为大家分享的GO语言中怎么实现Mysql数据库的CURD操作了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。