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)

}

posted @ 2021-11-07 00:11  读史  阅读(69)  评论(0编辑  收藏  举报