What database does golang use?
Today I will briefly introduce how Golang uses the mysql database. Since Go itself does not provide a specific database driver, it only provides driver interface and management. Each database driver needs to be implemented by a third party and registered in the driver management in Go. There are several mysql drivers on github. The one I chose here:
https://github.com/go-sql-driver/mysql
The following mainly introduces the use of mysql in go language from the aspects of addition, deletion, query, modification and things.
1. Connect to the database
Use the sql.Open() function Open the database connection. The database connection string (dsn) format is as follows:
admin:123456@tcp(10.2.1.5)/irisapp
The code is as follows:
func (f *mysql_db) mysql_open() { db, err := sql.Open("mysql", dbusername+":"+dbpassword+"@tcp("+dbhostsip+")/"+dbname) if err != nil { fmt.Println("链接失败") } fmt.Println("链接成功") f.db = db }
2. Insert
func (f *mysql_db) mysql_insert() { //insert 添加数据 fmt.Println("开始插入") stmt, err := f.db.Prepare("INSERT INTO depart(departName,memo) VALUES(?,?)") //defer stmt.Close() if err != nil { fmt.Println("插入失败") return } stmt.Exec("dd", "adadcccda") fmt.Println("插入成功") }
3. Query
func (f *mysql_db) mysql_select(sql_data string) { //select 查询数据 fmt.Println("sql:", sql_data) rows, err := f.db.Query(sql_data) if err != nil { fmt.Println("查询失败") } for rows.Next() { var departID int var departName string var memo string err = rows.Scan(&departID, &departName, &memo) if err != nil { panic(err) } fmt.Println("departName:", departName) } }
4. Update
func (f *mysql_db) mysql_update() { //update 修改数据 stmt, err := f.db.Prepare("update depart set departName=?,memo =? where departId=?") //defer stmt.Close() if err != nil { //错误处理 } result,_ := stmt.Exec("aa","asdfadsadsfa",1) if result == nil { fmt.Println("修改失败") } affect_count,_ := result.RowsAffected() //返回影响的条数,注意有两个返回值 fmt.Println("%v",affect_count) }
5. Delete
func (f *mysql_db) mysql_delete() { //delete 删除数据 stmt, err := f.db.Prepare("delete from depart where departId=?") //defer stmt.Close() if err != nil { //错误处理 } stmt.Exec(2) //不返回任何结果 fmt.Println("删除成功") }
6. Things
func (f *mysql_db) mysql_tran(){ //事务 tx,err := f.db.Begin() //声明一个事务的开始 if err != nil { fmt.Println(err) return } insert_sql := "insert into depart (departName,memo) VALUES(?,?)" insert_stmt,insert_err := tx.Prepare(insert_sql) if insert_err != nil { fmt.Println(insert_err) return } insert_res,insert_err := insert_stmt.Exec("ff","ff") last_insert_id,_ := insert_res.LastInsertId() fmt.Println(last_insert_id) // defer tx.Rollback() //回滚之前上面的last_login_id是有的,但在回滚后该操作没有被提交,被回滚了,所以上面打印的Last_login_id的这条数据是不存在与数据库表中的 tx.Commit() //这里提交了上面的操作,所以上面的执行的sql 会在数据库中产生一条数据 }
Call
func main() { db := &mysql_db{} db.mysql_open() db.mysql_insert() db.mysql_update() db.mysql_delete() db.mysql_tran() db.mysql_select("select departID,departName,memo from depart") db.mysql_close() //关闭 }
The output after startup is as follows:
D:\Go_Path\go\src\mysqldemo>go run mysqldemo.go 链接成功 开始插入 插入成功 0 删除成功 10 sql: select departID,departName,memo from depart departName: aa departName: dd departName: dd departName: dd departName: dd departName: dd departName: ff departName: dd departName: ff
Complete code
package main import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" ) var ( dbhostsip = "10.2.1.5:3306" dbusername = "admin" dbpassword = "123456" dbname = "irisapp" ) type mysql_db struct { db *sql.DB } func (f *mysql_db) mysql_open() { db, err := sql.Open("mysql", dbusername+":"+dbpassword+"@tcp("+dbhostsip+")/"+dbname) if err != nil { fmt.Println("链接失败") } fmt.Println("链接成功") f.db = db } func (f *mysql_db) mysql_close() { defer f.db.Close() } func (f *mysql_db) mysql_select(sql_data string) { //select 查询数据 fmt.Println("sql:", sql_data) rows, err := f.db.Query(sql_data) if err != nil { fmt.Println("查询失败") } for rows.Next() { var departID int var departName string var memo string err = rows.Scan(&departID, &departName, &memo) if err != nil { panic(err) } fmt.Println("departName:", departName) } } func (f *mysql_db) mysql_insert() { //insert 添加数据 fmt.Println("开始插入") stmt, err := f.db.Prepare("INSERT INTO depart(departName,memo) VALUES(?,?)") //defer stmt.Close() if err != nil { fmt.Println("插入失败") return } stmt.Exec("dd", "adadcccda") fmt.Println("插入成功") } func (f *mysql_db) mysql_update() { //update 修改数据 stmt, err := f.db.Prepare("update depart set departName=?,memo =? where departId=?") //defer stmt.Close() if err != nil { //错误处理 } result,_ := stmt.Exec("aa","asdfadsadsfa",1) if result == nil { fmt.Println("修改失败") } affect_count,_ := result.RowsAffected() //返回影响的条数,注意有两个返回值 fmt.Println(affect_count) } func (f *mysql_db) mysql_delete() { //delete 删除数据 stmt, err := f.db.Prepare("delete from depart where departId=?") //defer stmt.Close() if err != nil { //错误处理 } stmt.Exec(2) //不返回任何结果 fmt.Println("删除成功") } func (f *mysql_db) mysql_tran(){ //事务 tx,err := f.db.Begin() //声明一个事务的开始 if err != nil { fmt.Println(err) return } insert_sql := "insert into depart (departName,memo) VALUES(?,?)" insert_stmt,insert_err := tx.Prepare(insert_sql) if insert_err != nil { fmt.Println(insert_err) return } insert_res,insert_err := insert_stmt.Exec("ff","ff") last_insert_id,_ := insert_res.LastInsertId() fmt.Println(last_insert_id) // defer tx.Rollback() //回滚之前上面的last_login_id是有的,但在回滚后该操作没有被提交,被回滚了,所以上面打印的Last_login_id的这条数据是不存在与数据库表中的 tx.Commit() //这里提交了上面的操作,所以上面的执行的sql 会在数据库中产生一条数据 } func main() { db := &mysql_db{} db.mysql_open() db.mysql_insert() db.mysql_update() db.mysql_delete() db.mysql_tran() db.mysql_select("select departID,departName,memo from depart") db.mysql_close() //关闭 }
Summary
1. Supports preparation expressions, which can be used to optimize SQL queries to improve performance and reduce the risk of SQL injection, db.Prepare() and tx.Prepare provides support for prepared expressions.
2. LastInsertId() Get the id of the first inserted item
3. RowsAffected() Get the number of affected/inserted items
4. Here is just a brief introduction to go The basic use of MySQL in Go language development in language development. In fact, in the actual development process, ORM-related third-party frameworks are mainly used, but the underlying principles still need to be learned.
For more go language knowledge, please pay attention to the go language tutorial column on the PHP Chinese website.
The above is the detailed content of What database does golang use?. For more information, please follow other related articles on the PHP Chinese website!