数据库
首页 > 数据库> > go语言操作mysql数据库

go语言操作mysql数据库

作者:互联网

golang操作mysql数据库

go get github.com/go-sql-driver/mysql

package main

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

type Class struct {
	id int
	name string
}
go

func main() {
	db, _ := sql.Open("mysql", "root:12345@/test1")
	defer db.Close()
	//result,err:=db.Exec("insert into student_class ( class_name) VALUES (?)","class1")
	//fmt.Println(result)
	//fmt.Println(err)
	rows,err:=db.Query("select * from student_class")
	fmt.Println(rows)
	fmt.Println(err)
	defer rows.Close()
	class1:=&Class{}
	for rows.Next() {
		rows.Scan(&class1.id,&class1.name)
	}
	fmt.Println(class1)

}

标签:rows,数据库,Println,mysql,go,fmt,class1
来源: https://www.cnblogs.com/jackjewl/p/15518838.html