基本连接
package main
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type User struct {
Id int64 `db:"id"`
Name sql.NullString `db:"name"` // The username
Type int64 `db:"type"` // The user type, 0:normal,1:vip, for test golang keyword
CreateAt sql.NullTime `db:"create_at"`
UpdateAt time.Time `db:"update_at"`
}
func InitSQLXModels() {
dsn := "root:chenhaifeng@tcp(47.120.55.99:3306)/go_netdisk?charset=utf8mb4&parseTime=True&loc=Local"
conn := sqlx.NewMysql(dsn)
_ = conn
r, err := conn.ExecCtx(context.Background(), "insert into user (type, name) values (?, ?)", 1, "test")
if err != nil {
panic(err)
}
fmt.Println(r.RowsAffected())
}