Go操作Mysql基础-

package main

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

func main() {
var product model.Product
db, err := sql.Open("mysql", "root:123456@tcp(localhost:3306)/laravel_admin?charset=utf8")
if err != nil {
fmt.Println(">>> fail to connect to db <<<")
}
defer db.Close()

fmt.Println(">>> succeed to connect to db <<<")

err = db.QueryRow("select `name`,`price`,`created_at`,`intro`,`id` from product where id =?", 4).Scan(&product.Name, &product.Price, &product.CreatedAt, &product.Intro, &product.Id)

if err != nil {
fmt.Println(err)
}
fmt.Println(product.Name.String)
}
上一篇:java控制台订单系统


下一篇:Java项目:化妆品商城系统(java+Springboot+ssm+mysql+jsp+maven)