当前位置:网站首页>Go 访问GBase 8a 数据库的一个方法
Go 访问GBase 8a 数据库的一个方法
2022-06-27 19:33:00 【生命之源;】
截至目前(2022-05-26),GBase 8a尚未发布go语言的驱动,本文尝试通过go-sql-driver/mysql驱动来访问GBase 8a数据库。
Go 驱动安装
从github.com下载驱动
E:\go\hello>go get github.com/go-sql-driver/mysql
go: downloading github.com/go-sql-driver/mysql v1.6.0
go: added github.com/go-sql-driver/mysql v1.6.0
Go 代码
来自于官网的样例 https://go.dev/doc/tutorial/database-access
注意其中增加了 AllowNativePasswords: true 的配置,否则会报错
[mysql] 2022/05/26 14:46:43 connector.go:95: could not use requested auth plugin 'mysql_native_password': this user requires mysql native password authentication.
2022/05/26 14:46:43 this user requires mysql native password authentication.
package main
import (
"database/sql"
"fmt"
"log"
"os"
"github.com/go-sql-driver/mysql"
)
var db *sql.DB
type TableT1 struct {
ID int64
NAME string
}
func main() {
// Capture connection properties.
cfg := mysql.Config{
User: os.Getenv("DBUSER"),
Passwd: os.Getenv("DBPASS"),
Net: "tcp",
Addr: "192.168.56.1:5258",
DBName: "testdb",
AllowNativePasswords: true,
}
// Get a database handle.
var err error
db, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
log.Fatal(err)
}
pingErr := db.Ping()
if pingErr != nil {
log.Fatal(pingErr)
}
fmt.Println("Connected!")
tableT1s, err := selectTable("1")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Albums found: %v\n", tableT1s)
}
func selectTable(name string) ([]TableT1, error) {
// An tableT1s slice to hold data from returned rows.
var tableT1s []TableT1
rows, err := db.Query("SELECT * FROM t1 WHERE id = ?", name)
if err != nil {
return nil, fmt.Errorf("selectTable %q: %v", name, err)
}
defer rows.Close()
// Loop through rows, using Scan to assign column data to struct fields.
for rows.Next() {
var alb TableT1
if err := rows.Scan(&alb.ID,&alb.NAME); err != nil {
return nil, fmt.Errorf("selectTable %q: %v", name, err)
}
tableT1s = append(tableT1s, alb)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("selectTable %q: %v", name, err)
}
return tableT1s, nil
}
GBase 8a表
2个字段的表,没有采用官网的表结构
gbase> desc testdb.t1;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | varchar(100) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (Elapsed: 00:00:00.00)
gbase> select * from testdb.t1 where id=1;
+------+---------+
| id | name |
+------+---------+
| 1 | Name_10 |
+------+---------+
1 row in set (Elapsed: 00:00:00.01)
执行
E:\go\hello>go run .
Connected!
Albums found: [{1 Name_10}]
E:\go\hello>
更多精彩内容请点击https://www.gbase8.cn/9493
边栏推荐
- MySQL performance optimization index function, hidden, prefix, hash index usage (2)
- Go from introduction to actual combat - context and task cancellation (notes)
- Go from introduction to practice -- coordination mechanism (note)
- 有时间看看ognl表达式
- 本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
- Go from starting to Real - Interface (note)
- GFS分布式文件系统
- 鲜为人知的mysql导入数据
- Go从入门到实战——多态(笔记)
- Focus! Tips for installing fonts on domestic computers
猜你喜欢
随机推荐
跟我一起AQS SOS AQS
Go from introduction to actual combat - context and task cancellation (notes)
CEPH distributed storage
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
Knowledge sorting of exception handling
Go从入门到实战——package(笔记)
GFS分布式文件系统
Go从入门到实战——所有任务完成(笔记)
JVM memory structure when creating objects
ABC-Teleporter Setting-(思维+最短路)
uniapp拦截请求
VMware vSphere esxi 7.0 installation tutorial
SQL server for circular usage
What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?
AI 绘画极简教程
Go从入门到实战——CSP并发机制(笔记)
Codeforces Round #716 (Div. 2)
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
Industry case | see the operation of bank digital transformation from the king of retail
安装gatewayworker之后启动start.php









