README ¶
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.
Documentation ¶
Index ¶
Constants ¶
const Date = "2006-01-02"
Date is a standard date format understood by sqlanywhere
const DateTime = "2006-01-02 15:04:05.9"
DateTime The Go time format .9 fractional seconds accepts any number of fractional second digits (up to 9)
const DriverErrorCodeEOF = 100
DriverErrorCodeEOF is the error code representing end of results
const DriverName = "sqlanywhere"
DriverName is the registered name of this driver
const Time = "15:04:05.9"
Time is a standard time format understood by sqlanywhere
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
Driver is the interface to the database c api defined in sacapi.h
type DriverError ¶
type DriverError struct {
// contains filtered or unexported fields
}
DriverError is an error returned by calls to a connection
func (*DriverError) Error ¶
func (err *DriverError) Error() string