Golang 语言遍历Oceanbase数据库

1.模式Ocral

package main2

import (
"database/sql"
"fmt"
"net"
"time"

_ "github.com/aliyun/go-driver-ob"

)

func PingTCP(addr string) error {
conn, err := net.Dial("tcp", addr)
if err != nil {
return err
}
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(4 * time.Second))
reply := make([]byte, 128)
_, err = conn.Read(reply)

return err
}

func openDb() ( *sql.DB, error){

addr := fmt.Sprintf("%s:%s@tcp(%s)/", "name", "password", "t1.oceanbase.aliyuncs.com:1521")
err := PingTCP("t1.oceanbase.aliyuncs.com:1521")
if err != nil {
return nil, err
}

db, err := sql.Open("oceanbase", addr)
if err != nil {
return nil, err
}

return db, nil
}


func getColums(table string, db *sql.DB) error {

sql := fmt.Sprintf("select COLUMN_NAME, DATA_TYPE from user_tab_columns where table_name = ‘%s‘", table)
rows, err := db.Query(sql)
if err != nil {
fmt.Sprintf("db.Query %v", err)
return err
}

for rows.Next() {
var ColumnName string
var DataType string
err=rows.Scan(&ColumnName, &DataType)
fmt.Println("ColumnName:", ColumnName, "DataType:", DataType)
}
return err
}

func getData(table string, db *sql.DB) error {

sql := fmt.Sprintf("select * from %s where rownum<=2000", table)
rows, err := db.Query(sql)
if err != nil {
fmt.Sprintf("db.Raw(sql).Rows() %v", err)
return err
}
columns, err := rows.Columns()
if err != nil {
fmt.Sprintf("rows.Columns() %v", err)
return err
}
fmt.Println(columns)
count := len(columns)

for rows.Next() {
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
for i := 0; i < count; i++ {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
for i, col := range columns {
var v interface{}
val := values[i]
b, ok := val.([]byte)
if ok {
v = string(b)
} else {
v = val
}
fmt.Println("ColumnName:", col, "Data:", v)

}
}

return err
}

func listTable(db *sql.DB) error{
//var tbs []TableMeta
rows, err := db.Query("select TABLE_NAME,NUM_ROWS from user_tables")

if err != nil {
fmt.Println("select TABLE_NAME,NUM_ROWS from user_tables failed. ")
return err
}
for rows.Next() {
var TablesName string
var TablesRows int64
err=rows.Scan(&TablesName, &TablesRows)
fmt.Println("Tables name: ", TablesName)
fmt.Println("Tables Row: ", TablesRows)
getColums(TablesName,db)
getData(TablesName, db)
}

return nil
}

func main(){

db,err := openDb()
if err == nil {
listTable(db)
db.Close()
} else {
fmt.Println("open failed.")
}

}

 

2.模式Mysqlpackage main


import (
"database/sql"
"fmt"
"net"
"time"

_ "github.com/aliyun/go-driver-ob"

)


func PingTCP(addr string) error {
conn, err := net.Dial("tcp", addr)
if err != nil {
return err
}
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(4 * time.Second))
reply := make([]byte, 128)
_, err = conn.Read(reply)

return err
}

func openDb() ( *sql.DB, error){


addr := fmt.Sprintf("%s:%s@tcp(%s)/sddptest02", "USER", "PASSWORD", "t12.oceanbase.aliyuncs.com:3306")
err := PingTCP("t12.oceanbase.aliyuncs.com:3306")
if err != nil {
return nil, err
}

db, err := sql.Open("oceanbase", addr)
if err != nil {
return nil, err
}

return db, nil
}


func getColums(table string, db *sql.DB) error {

sql := fmt.Sprintf("show columns from %s.%s", "sddptest02", table)
rows, err := db.Query(sql)
if err != nil {
fmt.Sprintf("db.Query %v", err)
return err
}

for rows.Next() {
var ColumnName string
var DataType string
var NullFlag string
var KeyFlag string
var DefaultFlag string
var ExtraFlag string
err=rows.Scan(&ColumnName, &DataType, &NullFlag, &KeyFlag, &DefaultFlag, &ExtraFlag)
fmt.Println("ColumnName:", ColumnName, "DataType:", DataType)
}
return err
}

func getData(table string, db *sql.DB) error {

sql := fmt.Sprintf("select * from %s limit 2000", table)
rows, err := db.Query(sql)
if err != nil {
fmt.Sprintf("db.Raw(sql).Rows() %v", err)
return err
}
columns, err := rows.Columns()
if err != nil {
fmt.Sprintf("rows.Columns() %v", err)
return err
}
fmt.Println(columns)
count := len(columns)

for rows.Next() {
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
for i := 0; i < count; i++ {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
for i, col := range columns {
var v interface{}
val := values[i]
b, ok := val.([]byte)
if ok {
v = string(b)
} else {
v = val
}
fmt.Println("ColumnName:", col, "Data:", v)

}
}

return err
}

func listTable(db *sql.DB) error{
//var tbs []TableMeta
rows, err := db.Query("show tables from sddptest02")

if err != nil {
fmt.Println("show tables. ")
return err
}
for rows.Next() {
var TablesName string
err=rows.Scan(&TablesName)
fmt.Println("Tables name: ", TablesName)
getColums(TablesName,db)
getData(TablesName, db)
}

return nil
}

func insertData(db *sql.DB) {

for i :=0; i< 200; i++ {
sql := fmt.Sprintf("insert into table1 (name,address) values (‘zhangsan%d‘,‘beijing%d‘)", i, i)
if result, err := db.Exec(sql); err == nil {
if num, err := result.RowsAffected(); err == nil {
fmt.Println("insert row number : ", num);
} else {
fmt.Println("affected row number failed ", err)
}
} else {
fmt.Println("insert row number failed ", err)
}
}

}
func main(){

db,err := openDb()
if err == nil {
insertData(db)
listTable(db)
db.Close()
} else {
fmt.Println("open failed.")
}

}

 

Golang 语言遍历Oceanbase数据库

上一篇:mysql数据库性能优化(包括SQL,表结构,索引,缓存)


下一篇:关于jQuery的.data()方法,重新赋值后好像不起作用