代码示例:

mysql.go

package main

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

func main(){

	db, err := sql.Open("mysql", "root:369369@tcp(localhost:3306)/aic_develop")

	if err!=nil{
		log.Fatal(err)
	}

	defer db.Close()	//关闭数据库连接

	var (
		id int
		dataelementid string
	)

	rows, err := db.Query("select  id, dataelementid from c_db_dataelement ")

	if err!=nil{
		log.Fatal(err)
	}

	defer rows.Close()

	for rows.Next() {
		err := rows.Scan(&id, &dataelementid)
		if err != nil {
			log.Fatal(err)
		}
		log.Println(id, dataelementid)
	}
	err = rows.Err()
	if err!=nil{
		log.Fatal(err)
	}

}