当前位置:网站首页>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
边栏推荐
- At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
- Go从入门到实战——接口(笔记)
- 微服务之远程调用
- SQL必需掌握的100个重要知识点:使用函数处理数据
- 100 important knowledge points for SQL: in operator
- GBase 8a OLAP函数group by grouping sets的使用样例
- 01-Golang-环境搭建
- 豆沙绿保护你的双眼
- Codeforces Round #723 (Div. 2)
- 让马化腾失望了!Web3.0,毫无希望
猜你喜欢
集合代码练习
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen
Bit. Store: long bear market, stable stacking products may become the main theme
Go from introduction to actual combat - context and task cancellation (notes)
Go从入门到实战——错误机制(笔记)
PCIE知识点-008:PCIE switch的结构
Galaxy Kirin system LAN file sharing tutorial
Let Ma Huateng down! Web3.0, hopeless
Here are 12 commonly used function formulas for you. All used ones are good
How to participate in openharmony code contribution
随机推荐
Unleash the innovative power of open source database | [Gansu] opengauss meetup has come to a successful conclusion
MySQL usage notes 1
Ceph分布式存储
Codeforces Round #719 (Div. 3)
Go从入门到实战——依赖管理(笔记)
Tiktok's interest in e-commerce has hit the traffic ceiling?
Go从入门到实战——接口(笔记)
Go从入门到实战——仅执行一次(笔记)
Prospects for enterprise digitalization (38/100)
语言弱点列表--CWE,一个值得学习的网站
如何将队列里面的内容没秒钟执行一次优先级
Go从入门到实战——任务的取消(笔记)
100 important knowledge points that SQL must master: sorting and retrieving data
Go from introduction to actual combat - all tasks completed (notes)
Null pointer exception
How to participate in openharmony code contribution
Squid proxy server
Serveur mandataire SQUID
Go从入门到实战——仅需任意任务完成(笔记)
Icml2022 | scalable depth Gaussian Markov random field