driver

command
v0.0.0-...-3c93513 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2023 License: Apache-2.0, Apache-2.0 Imports: 13 Imported by: 0

README

go-driver

Go数据库驱动

编译

Mysql

  • Docker 运行MySql
# 下载镜像
docker pull mysql:5.7
#运行一个容器
docker run -itd --name mysql-3306 -m 1024m -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
#停止容器
docker stop mysql-3306
#删除容器
docker rm mysql-3306
  • 程序调用
package main

import (
  "fmt"
  "sorm/driver/db_mysql"
  "testing"
)

func TestMysql(t *testing.T) {

  dsn := db_mysql.GetDSN("root", "123456", "127.0.0.1", 3306, "")
  db, err := db_mysql.Open(dsn)
  if err != nil {
    panic(err)
  }
  sql := `select 2`
  var count int
  rows, err := db.Query(sql)
  if err != nil {
    panic(err)
  }
  rows.Next()
  err = rows.Scan(&count)
  if err != nil {
    panic(err)
  }
  fmt.Printf("result:%d\n", count)
  if count == 2 {
    fmt.Println("test success")
  } else {
    panic("test fail")
  }
}

Sqlite3

  • 程序调用
package main

import (
  "fmt"
  "sorm/driver/db_sqlite3"
  "testing"
)

func TestSqlite3(t *testing.T) {

  dsn := db_sqlite3.GetDSN("test-sqlite3")
  db, err := db_sqlite3.Open(dsn)
  if err != nil {
    panic(err)
  }
  sql := `select 2`
  var count int
  rows, err := db.Query(sql)
  if err != nil {
    panic(err)
  }
  rows.Next()
  err = rows.Scan(&count)
  if err != nil {
    panic(err)
  }
  fmt.Printf("result:%d\n", count)
  if count == 2 {
    fmt.Println("test success")
  } else {
    panic("test fail")
  }
}

达梦

  • Docker 运行达梦数据库
# 下载镜像
docker pull teamide/dm8:v201106
#运行一个容器
docker run -itd --name dm-5236 -p 5236:5236 teamide/dm8:v201106
#停止容器
docker stop dm-5236
#删除容器
docker rm dm-5236

# 端口: 5236
# 用户名: SYSDBA
# 密码: SYSDBA
  • 程序调用
package main

import (
  "fmt"
  "sorm/driver/db_dm"
  "testing"
)

func TestDm(t *testing.T) {
  dsn := db_dm.GetDSN("SYSDBA", "SYSDBA", "127.0.0.1", 5236)
  db, err := db_dm.Open(dsn)
  if err != nil {
    panic(err)
  }
  sql := `select 2`
  var count int
  rows, err := db.Query(sql)
  if err != nil {
    panic(err)
  }
  rows.Next()
  err = rows.Scan(&count)
  if err != nil {
    panic(err)
  }
  fmt.Printf("result:%d\n", count)
  if count == 2 {
    fmt.Println("test success")
  } else {
    panic("test fail")
  }
}

金仓

  • Docker 运行金仓数据库
# 下载镜像
docker pull teamide/kingbase:v8r3
#运行一个容器
docker run -itd --name kingbase-54321 -p 54321:54321 -e SYSTEM_USER=SYSTEM -e SYSTEM_PWD=123456 teamide/kingbase:v8r3
#停止容器
docker stop kingbase-54321
#删除容器
docker rm kingbase-54321

# 端口: 54321
# 用户名: SYSTEM
# 密码: 123456
# 默认数据库: TEST
  • 程序调用
package main

import (
  "fmt"
  "sorm/driver/db_kingbase_v8r6"
  "testing"
)

func TestKingBase(t *testing.T) {
  dsn := db_kingbase_v8r6.GetDSN("SYSTEM", "123456", "127.0.0.1", 54321, "TEST")
  db, err := db_kingbase_v8r6.Open(dsn)
  if err != nil {
    panic(err)
  }
  sql := `select 2`
  var count int
  rows, err := db.Query(sql)
  if err != nil {
    panic(err)
  }
  rows.Next()
  err = rows.Scan(&count)
  if err != nil {
    panic(err)
  }
  fmt.Printf("result:%d\n", count)
  if count == 2 {
    fmt.Println("test success")
  } else {
    panic("test fail")
  }
}

神通

  • Docker 运行神通数据库
# 下载镜像
docker pull teamide/shentong:7.0.8
#运行一个容器
docker run -itd --name shentong-2003 -p 2003:2003 teamide/shentong:7.0.8
#停止容器
docker stop shentong-2003
#删除容器
docker rm shentong-2003

# 端口: 2003
# 用户名: sysdba
# 密码: szoscar55
# 默认数据库: OSRDB
  • 环境配置

window 环境配置

# 配置 动态链接库
# 将go-driver/driver/shentong/go-aci/lib/win64/aci.dll 复制 到 C:/Windows/System32 下
# 或 将 aci.dll 放在程序目录

linux 环境配置

# 将go-driver/driver/shentong/go-aci/lib/linux64/libaci.so 复制 到 /usr/lib64/ 下
# 或 配置 动态链接库
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:go-driver/driver/shentong/go-aci/lib/linux64/
  • 程序调用
package main

import (
  "fmt"
  "sorm/driver/db_shentong"
  "testing"
)

func TestShenTong(t *testing.T) {

  dsn := db_shentong.GetDSN("SYSDBA", "szoscar55", "127.0.0.1", 2003, "OSRDB")
  db, err := db_shentong.Open(dsn)
  if err != nil {
    panic(err)
  }
  sql := `select 2`
  var count int
  rows, err := db.Query(sql)
  if err != nil {
    panic(err)
  }
  rows.Next()
  err = rows.Scan(&count)
  if err != nil {
    panic(err)
  }
  fmt.Printf("result:%d\n", count)
  if count == 2 {
    fmt.Println("test success")
  } else {
    panic("test fail")
  }
}

Oracle

  • Docker 运行Oracle数据库
# 下载镜像
docker pull teamide/oracle-xe-11g:1.0
#运行一个容器
docker run -itd --name oracle-1521 -p 1521:1521 teamide/oracle-xe-11g:1.0
#停止容器
docker stop oracle-1521
#删除容器
docker rm oracle-1521

# 端口: 1521
# 用户名: root
# 密码: 123456
# SID: xe

window 环境配置

  • 方案一:
    1. 安装oracle 64位客户端
    2. 配置环境path,将客户端的目录,比如 c:\oracle\product\11.2.0\client_1\bin 配置到path中
    3. 配置环境变量ORACLE_HOME目录设置为如 c:\oracle\product\11.2.0\client_1 后,问题解决。
  • 方案二:
    1. 下载轻量级的Oracle客户端: 比如 instantclient-basic-windows.x64-11.2.0.4.0.zip
    2. 配置环境变量path:将客户端的目录,比如 c:\oracle\instantclient_11_2 添加到 path
    3. 配置环境变量ORACL_HOME: 将客户端的上一级目录 ,比如 c:\oracle 设置为ORACLE_HOME的值

linux 环境配置

  • 方案一:

    1. 下载轻量级的oracle客户端并解压安装
    2. 配置环境变量LD_LIBRARY_PATH=[oracle客户端安装目录]:$LD_LIBRARY_PATH ,NLS_LANG=[oracle数据库NLS_LANGUAGE_NLS_TERRITORY.NLS_CHARACTERSET] ,并添加到 ~/.bash_profile 中
    3. 重新登录系统 或 source ~/.bash_profile 使环境变量生效
  • 程序调用

package main

import (
  "fmt"
  "sorm/driver/db_oracle"
  "testing"
)

func TestOracle(t *testing.T) {

  dsn := db_oracle.GetDSN("root", "123456", "127.0.0.1", 1521, "xe")
  db, err := db_oracle.Open(dsn)
  if err != nil {
    panic(err)
  }
  sql := `select 2 from dual`
  var count int
  rows, err := db.Query(sql)
  if err != nil {
    panic(err)
  }
  rows.Next()
  err = rows.Scan(&count)
  if err != nil {
    panic(err)
  }
  fmt.Printf("result:%d\n", count)
  if count == 2 {
    fmt.Println("test success")
  } else {
    panic("test fail")
  }
}

Postgresql

  • Docker 运行Postgresql数据库
# 下载镜像
docker pull postgres:11.14
#运行一个容器
docker run -itd --name postgres-5432 -p 5432:5432 -e POSTGRES_PASSWORD=123456 postgres:11.14
#停止容器
docker stop postgres-5432
#删除容器
docker rm postgres-5432

# 端口: 5432
# 用户名: postgres
# 密码: 123456
# 默认数据库: postgres
  • 程序调用
package main

import (
  "fmt"
  "sorm/driver/db_postgresql"
  "testing"
)

func TestPostgresql(t *testing.T) {

  dsn := db_postgresql.GetDSN("postgres", "123456", "127.0.0.1", 5432, "postgres")
  db, err := db_postgresql.Open(dsn)
  if err != nil {
    panic(err)
  }
  sql := `select 2`
  var count int
  rows, err := db.Query(sql)
  if err != nil {
    panic(err)
  }
  rows.Next()
  err = rows.Scan(&count)
  if err != nil {
    panic(err)
  }
  fmt.Printf("result:%d\n", count)
  if count == 2 {
    fmt.Println("test success")
  } else {
    panic("test fail")
  }
}

GBase

  • Docker 运行GBase数据库
# 下载镜像
docker pull teamide/gbase8s:3.3.0_2_amd64
#运行一个容器
docker run -itd --name gbase-9088 -p 9088:9088 teamide/gbase8s:3.3.0_2_amd64
#停止容器
docker stop gbase-9088
#删除容器
docker rm gbase-9088

# 端口: 9088
# 用户名: gbasedbt
# 密码: GBase123
# 默认数据库: OSRDB

# 下载 https://gbasedbt.com/dl/odbc/GBase8s_3.0-Win64-ODBC-Driver.zip

# 解压
# 使用管理员权限运行00注册ODBC_管理员权限运行.cmd,注册ODBC驱动

# 打开window odbc配置,添加GBase驱动


  • 程序调用
package main

import (
  "fmt"
  "sorm/driver/db_gbase"
  "testing"
)

func TestGBase(t *testing.T) {
  dsn := `DRIVER=com.gbasebt.jdbc.Driver;NEWCODESET=UTF8,zh_cn.UTF8,57372;DB_LOCALE=zh_cn.57372;DELIMIDENT=Y;CLIENT_LOCALE=zh_cn.57372;ServerName=gbase01;host=127.0.0.1;service=9088;uid=gbasedbt;pwd=GBase123;DATABASE=JOB1;`
  db, err := db_gbase.Open(dsn)
  if err != nil {
    panic(err)
  }
  sql := `select 2 from dual`
  var count int
  rows, err := db.Query(sql)
  if err != nil {
    panic(err)
  }
  rows.Next()
  err = rows.Scan(&count)
  if err != nil {
    panic(err)
  }
  fmt.Printf("result:%d\n", count)
  if count == 2 {
    fmt.Println("test success")
  } else {
    panic("test fail")
  }
}

OpenGauss

  • Docker 运行GBase数据库
# 下载镜像
docker pull enmotech/opengauss:3.1.0
# 运行一个容器
docker run -itd --name opengauss-5432 -p 5432:5432 -e GS_PASSWORD=Enmo@1234 enmotech/opengauss:3.1.0

# GS_PASSWORD:设置openGauss数据库的超级用户omm以及测试用户gaussdb的密码。如果要从容器外部(其它主机或者其它容器)连接则必须要输入密码。包含大写、小写、数字、特殊字符和密码长度(最小8)
# GS_NODENAME:数据库节点名称,默认为gaussdb。
# GS_USERNAME:数据库连接用户名,默认为gaussdb。
# GS_PORT:数据库端口,默认为5432

#停止容器
docker stop opengauss-5432
#删除容器
docker rm opengauss-5432

# 端口: 5432
# 用户名: gaussdb
# 密码: Enmo@1234
# 默认数据库: postgres
  • 程序调用
package db_opengauss

import (
  "fmt"
  "testing"
)

func TestDb(t *testing.T) {

  dsn := GetDSN("gaussdb", "Enmo@1234", "127.0.0.1", 5432, "postgres")
  db, err := Open(dsn)
  if err != nil {
    panic(err)
  }
  sql := `select 2`
  var count int
  rows, err := db.Query(sql)
  if err != nil {
    panic(err)
  }
  rows.Next()
  err = rows.Scan(&count)
  if err != nil {
    panic(err)
  }
  fmt.Printf("result:%d\n", count)
  if count == 2 {
    fmt.Println("test success")
  } else {
    panic("test fail")
  }
}


Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
driver
kingbase/v8r3/kingbase.com/gokb
Package kb is a pure Go Kingbase driver for the database/sql package.
Package kb is a pure Go Kingbase driver for the database/sql package.
kingbase/v8r3/kingbase.com/gokb/oid
Package oid contains OID constants as defined by the Kingbase server.
Package oid contains OID constants as defined by the Kingbase server.
kingbase/v8r3/kingbase.com/gokb/scram
Package scram implements a SCRAM-{SHA-1,etc} client per RFC5802.
Package scram implements a SCRAM-{SHA-1,etc} client per RFC5802.
kingbase/v8r6/kingbase.com/gokb
Package kb is a pure Go Kingbase driver for the database/sql package.
Package kb is a pure Go Kingbase driver for the database/sql package.
kingbase/v8r6/kingbase.com/gokb/oid
Package oid contains OID constants as defined by the Kingbase server.
Package oid contains OID constants as defined by the Kingbase server.
kingbase/v8r6/kingbase.com/gokb/scram
Package scram implements a SCRAM-{SHA-1,etc} client per RFC5802.
Package scram implements a SCRAM-{SHA-1,etc} client per RFC5802.
odbc
Package odbc implements database/sql driver to access data via odbc interface.
Package odbc implements database/sql driver to access data via odbc interface.
yxres/uxgo/oid
Package oid contains OID constants as defined by the UXres server.
Package oid contains OID constants as defined by the UXres server.
yxres/uxgo/scram
Package scram implements a SCRAM-{SHA-1,etc} client per RFC5802.
Package scram implements a SCRAM-{SHA-1,etc} client per RFC5802.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL