Skip to content

BenediktGerlach/div.go.benedikt-gerlach.sqlanywhere

 
 

Repository files navigation

SQLAnywhere Go Driver (Linux, CGO)

This repository is an implementation of a Go sql/driver to access SQLAnywhere databases. It uses SQLAnywhere's C api via cgo. The driver is intended for linux based client applications.

Compiling

Important: This is a CGO enabled package, and depends on shared libraries to compile and run.

Compilation requirements:

  • cgo enabled
  • environment variable CGO_LDFLAGS="-L /path/to/libs"
  • gcc compiler present in path

Runtime requirements:

  • environment variable LD_LIBRARY_PATH="/path/to/libs"

/path/to/libs is the full path to the directory containing sqlanywhere linux shared object library files. The file libdbcapi_r.so is an example.

The libraries are typically installed as part of the installation of sqlanywhere server. If you don't have an existing sqlanywhere server installation, you can install the time limited free trial sqlanywhere developer edition. In case the link is unreachable, a direct download is available (~320Mb).

This project uses docker and make for development. The Makefile has targets to download the sqlanywhere v17 developer edition locally once and install it in a container for compilation and testing.

Usage

Connect to a database with a standard sqlanywhere connection string.

For example:

package main

import (
    _ "github.com/mdcnz/sqlanywhere"
    "database/sql"
    "log"
)

func main() {
    db, err := sql.Open("sqlanywhere", "uid=DBA;pwd=xxx;Host=myhost;DBN=mydb;Server=myserver")
    if err != nil {
        log.Fatalf("did not open: %v", err)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        log.Fatalf("did not ping: %v", err)
    }
}

Running sqlanywhere server

Examples of starting a server in the background, and testing a connection using dbping:

dbspawn dbsrv17 -n sqlanywhere-db-server -su dba,sqlsql
dbping -d -c "uid=dba;pwd=sqlsql;server=sqlanywhere-db-server;dbn=utility_db"

Troubleshooting

/usr/bin/ld: cannot find -ldbcapi_r

/usr/bin/ld: cannot find -l:libdbcapi_r.so

This can occur at compile time if CGO_LDFLAGS is unset or incorrect.

error while loading shared libraries: libdbcapi_r.so: cannot open shared object file: No such file or directory

This can occur at runtime if the LD_LIBRARY_PATH is unset or incorrect.

About

SQLAnywhere Go Driver (CGO, Linux)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 49.9%
  • C 49.1%
  • Other 1.0%