Monkey

个人站

The master has failed more times than the beginner has tried


GO 操作 MySQL 02

  • 首先创建数据库及数据表
# 创建数据库 demo
CREATE DATABASE IF NOT EXISTS demo DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
# 创建数据表 student
CREATE TABLE IF NOT EXISTS `student`(
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL,
    `sex` varchar(40) NOT NULL,
    `age` int(11) DEFAULT NULL,
    `course` varchar(100) NOT NULL,
    PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • 插入操作

    • 建议使用结构化操作,不推荐使用直接拼接sql语句的方法。
package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

/**
	使用结构体数据
 */
type Student struct {
	Id     int    `db:"id"`
	Name   string `db:"name"`
	Sex    string `db:"sex"`
	Age    int    `db:"age"`
	Course string `db:"course"`
}

var Db *sqlx.DB

func init() {
	database, err := sqlx.Open("mysql", "root:root@tcp(127.0.0.1:3306)/demo")
	if err != nil {
		fmt.Println("connect fail...")
		return
	}
	Db = database
}

func main() {
	insert, err := Db.Exec("insert into `student` (`name`, `sex`, `age`, `course`) value (?, ?, ?, ?)", "张三", "男", 18, "Golang")
	if err != nil {
		fmt.Println("insert fail...", err)
		return
	}
	id, err := insert.LastInsertId()
	if err != nil {
		fmt.Println("insert fail...", err)
		return
	}
	fmt.Println("last insert id is : ", id)

}

  • 查询操作
package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

type Student struct {
	Id     int    `db:"id"`
	Name   string `db:"name"`
	Sex    string `db:"sex"`
	Age    int    `db:"age"`
	Course string `db:"course"`
}

var Db *sqlx.DB

func init()  {
	database, err := sqlx.Open("mysql", "root:root@tcp(127.0.0.1:3306)/demo")
	if err != nil {
		fmt.Println("连接错误")
	}
	Db = database
}

func main()  {
	var student []Student
	err := Db.Select(&student, "select * from student")
	if err != nil {
		fmt.Println("查询失败, ", err)
		return
	}

	fmt.Println("select succ:", student)
}
  • 更新操作
package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

type Student struct {
	Id int `db:"id"`
	Name string `db:"name"`
	Age int `db:"age"`
	Course string `db:"course"`
}

var Db *sqlx.DB

func init() {
	database, err := sqlx.Open("mysql", "root:root@tcp(127.0.0.1:3306)/demo")
	if err != nil {
		fmt.Println("连接失败")
		return
	}
	Db = database
}

func main() {
	data, err := Db.Exec("update student set course = ? where id = ?", "php", "2")
	if err != nil {
		fmt.Println("更新失败")
		return
	}
	row, err := data.RowsAffected()
	if err != nil {
		fmt.Println("操作失败", err)
		return
	}
	fmt.Println(row)
}

  • 删除操作
package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

type Student struct {
	Id int `db:"id"`
	Name string `db:"name"`
	Age int `db:"age"`
	Course string `db:"course"`
}

var Db *sqlx.DB

func init() {
	database, err := sqlx.Open("mysql", "root:root@tcp(127.0.0.1:3306)/demo")
	if err != nil {
		fmt.Println("connect fail ...")
		return
	}
	Db = database
}

func main() {
	data, err := Db.Exec("delete from student where id = ?", "1")
	if err != nil {
		fmt.Println("delete fail ...")
		return
	}
	row, err := data.RowsAffected()
	if err != nil {
		fmt.Println("delete fail ...")
		return
	}

	fmt.Println("delete affected ", row)
}

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦