当前位置:网站首页>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
边栏推荐
- Go从入门到实战——Panic和recover(笔记)
- Go从入门到实战—— 多路选择和超时控制(笔记)
- 专题教程——选队长游戏
- SQL必需掌握的100个重要知识点:IN 操作符
- 100 important knowledge points that SQL must master: using functions to process data
- Galaxy Kirin system LAN file sharing tutorial
- CEPH distributed storage
- Day8 ---- 云资讯项目介绍与创建
- SQL必需掌握的100个重要知识点:过滤数据
- Go from introduction to practice - polymorphism (note)
猜你喜欢

Null pointer exception

Go from entry to practice - multiple selection and timeout control (notes)

How to participate in openharmony code contribution

Knowledge sorting of exception handling

Go from introduction to actual combat - panic and recover (notes)

Express e stack - small items in array

"Apprendre cette image" apparaît sur le Bureau win11 comment supprimer

How to delete "know this picture" on win11 desktop

Use the storcli tool to configure raid. Just collect this article

squid代理服務器
随机推荐
Use the storcli tool to configure raid. Just collect this article
SQL必需掌握的100个重要知识点:使用函数处理数据
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
SQL server for circular usage
win11桌面出現“了解此圖片”如何删除
Go从入门到实战——行为的定义和实现(笔记)
SQL必需掌握的100个重要知识点:IN 操作符
oss上传调用的是哪个方法
excel读取文件内容方法
ARCS模型介绍
Tiktok's interest in e-commerce has hit the traffic ceiling?
Prospects for enterprise digitalization (38/100)
GoLand permanently activated
请教CMS小程序首页的幻灯片在哪里设置?
GFS分布式文件系统
数组作业题
How to delete "know this picture" on win11 desktop
squid代理服務器
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
分享|智慧环保-生态文明信息化解决方案(附PDF)