1、TiDB - 安装数据
[zls@archlinux tidb-docker-compose]$ mysql -h 127.0.0.1 -P 4000 -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 52
Server version: 5.7.10-TiDB-v3.0.0-beta-148-ge767c5b5b MySQL Community Server (Apache License 2.0)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>
2、TiDB - 创建数据库
> create database test;
3、TiDB - DEMO
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"testing"
"github.com/hb-go/gorm"
)
func TestXorm2TiDB(t *testing.T) {
db, err := xorm.NewEngine("mysql", "root:@tcp(172.18.0.1:4000)/test")
if err != nil {
fmt.Println(err.Error())
return
}
tabs, err := db.DBMetas()
for _, v := range tabs {
t.Log(v.Name)
}
var rows []map[string]string
if rows, err = db.QueryString("select * from test"); err != nil {
t.Error(err)
return
}
for k, v := range rows {
t.Log(k, v)
}
defer db.Close()
}
func TestGorm2TiDB(t *testing.T) {
db, err := gorm.Open("mysql", "root:@tcp(172.18.0.1:4000)/test")
if err != nil {
fmt.Println(err.Error())
return
}
rows, err := db.Raw("select * from test").Rows()
if err != nil {
t.Log(err)
}
defer rows.Close()
for rows.Next() {
var id int
var name string
rows.Scan(&id, &name)
t.Log(id, name)
}
defer db.Close()
}
有疑问加站长微信联系(非本文作者)