1. Index
  2. Shell
  3. C
  4. POSIX
  5. JavaScript

Open Database Connectivity

ODBC definiert eine gemeinsame Schnittstelle für den Zugriff auf Datenbanken verschiedener Hersteller.

Installation

UnixODBC verwendet dynamisch ladbare Bibliotheken als Treiber.

apt install unixodbc-dev libsqliteodbc …

Jeder Treiber für ein bestimmtes Datenbank-Managemement-System veranlasst einen Eintrag in die Konfigurationsdatei.

/etc/odbcinst.ini
[sqlite3] Description=SQLite3 ODBC Driver Driver=libsqlite3odbc.so Setup=libsqlite3odbc.so UsageCount=1

Eine weitere Konfigurationsdatei enthält Einträge mit den Verbindungs- und Zugangsdaten für bestimmte Datenquellen (DSNs).

/etc/odbc.ini
[demo] Description=Demo Database Driver=sqlite3 Database=/var/lib/sqlite/demo.db Timeout=2000

Beispiel

#include <sql.h>
#include <sqlext.h>

int
main (int argc, char * argv)
{
	SQLHENV     env;
	SQLHDBC     dbc;
	SQLHSTMT    stmt;
	SQLSMALLINT len;
	SQLLEN      ind;
	SQLCHAR     buf[BUFSIZ];

	if (SQL_SUCCEEDED (SQLAllocEnv (&env))
	&&  SQL_SUCCEEDED (SQLAllocConnect (env, &dbc))
	&&  SQL_SUCCEEDED (SQLDriverConnect (dbc, NULL,
			(SQLCHAR *) "DSN=demo", SQL_NTS,
			buf, sizeof (buf), &len, SQL_DRIVER_COMPLETE))) {
		SQLAllocStmt (dbc, &stmt);
		SQLExecDirect (stmt, (SQLCHAR *) "SELECT id FROM demo", SQL_NTS);
		while (SQL_SUCCEEDED (SQLFetch (stmt))) {
			SQLGetData (stmt, 1, SQL_C_CHAR,
					buf, sizeof (buf), &ind);
		}
		SQLFreeStmt (stmt, SQL_CLOSE);
	}
	SQLDisconnect  (dbc);
	SQLFreeConnect (dbc);
	SQLFreeEnv     (env);
	return 0;
}

Übersetzn und binden mit:

gcc -g -Wall -lodbc -o odbc odbc.c

API-Referenz

Handles
SQLHENV env
SQLHDBC dbc
SQLHSTMT stm
SQLHDESC dsc
Allgemein
SQLAllocHandle (type, input, * output);
SQLCancelHandle (type, input);
SQLFreeHandle (type, handle);
SQLGetDiagField (type, handle,
rec, identifier,
info, max, * length);
SQLGetDiagRec (type, handle,
rec, state, * native,
message, max, * length);
Environment
SQLAllocEnv (SQLHENV * env);
SQLFreeEnv (env);
SQLGetEnvAttr (env, attr, value, * length);
SQLSetEnvAttr (env, attr, value, length);
SQLDataSources (env, direction,
server, max1, * length1,
desc, max2, * length2);
SQLError (env, dbc, stm,
state, * native,
message, max, * length);
Connection
SQLAllocConnect (env, SQLHDBC * dbc);
SQLFreeConnect (dbc);
SQLConnect (dbc, server, length1,
username, length2,
password, length3);
SQLDisconnect (dbc);
SQLGetFunctions (dbc, func, * supported);
SQLGetInfo (dbc, type, info, max, * length);
SQLGetConnectAttr (dbc, attr, value, * length);
SQLSetConnectAttr (dbc, attr, value, length);
SQLGetConnectOption(dbc, option, value);
SQLSetConnectOption(dbc, option, value);
SQLTransact (env, dbc, completion);
SQLEndTran ( type, handle, completion);
Schema
SQLTables(stm, catalog, length1,
schema, length2,
table, length3,
type, length4);
SQLColumns(stm, catalog, length1,
schema, length2,
table, length3,
column, length4);
SQLSpecialColumns (stm, type,
catalog, length1,
schema, length2,
table, length3,
scope, nullable);
SQLDescribeCol (stm, column,
name, max, * length,
* type, * size,
* digits, * nullable);
SQLColAttribute (stm, column, field,
charattr, max, * length,
* num_attr);
SQLGetTypeInfo (stm, type);
SQLStatistics (stm, catalog, length1,
schema, length2,
table, length3,
unique, reserved);
Descriptor
SQLGetDescField (dsc, rec, field, value, * length);
SQLSetDescField (dsc, rec, field, value, max);
SQLGetDescRec (dsc, rec, name, max, * length,
* type,
* subtype,
* length,
* precision,
* scale,
* nullable);
SQLSetDescRec (dsc, rec,
type,
subtype,
length,
precision,
scale,
data, * length2, * indicator);
SQLCopyDesc (dsc, SQLHDESC target);
Statement
SQLAllocStmt (dbc, SQLHSTMT * stm);
SQLFreeStmt (stm, opt);
SQLExecDirect (stm, statement, length);
SQLGetStmtAttr (stm, attr, value, * length);
SQLSetStmtAttr (stm, attr, value, length);
SQLGetStmtOption (stm, option, value);
SQLSetStmtOption (stm, option, value);
Bind
SQLPrepare (stm, statement, length);
SQLSetParam (stm, number, vtype, ptype, precision,
scale, value, * indicator);
SQLParamData (stm, * value);
SQLBindParam (stm, number, vtype, ttype, precision,
scale, value, * indicator);
SQLBindCol (stm, column, ttype,
target, max, * indicator);
SQLExecute (stm);
SQLCancel (stm);
Fetch
SQLNumResultCols (stm, * count);
SQLRowCount (stm, * count);
SQLFetch (stm);
SQLFetchScroll (stm, orientation, offset);
SQLGetData (stm, column, ttype,
target, max, * indicator);
SQLPutData (stm, source, indicator);
Cursor
SQLGetCursorName (stm, cursor, max, * length);
SQLSetCursorName (stm, cursor, length);
SQLCloseCursor (stm);
SQL_HANDLE_ENV
SQL_HANDLE_DBC
SQL_HANDLE_STMT
SQL_HANDLE_DESC
SQL_ROW_IDENTIFIER
SQL_MAX_DRIVER_CONNECTIONS
SQL_OUTER_JOIN_CAPABILITIES
ParameterType
SQL_C_CHAR
SQL_C_SHORT
SQL_C_LONG
SQL_C_FLOAT
SQL_C_DOUBLE
SQL_PARAM_TYPE_UNKNOWN
SQL_PARAM_INPUT
SQL_PARAM_OUTPUT
SQL_PARAM_INPUT_OUTPUT
SQL_API_SQLALLOCCONNECT
SQL_API_SQLTABLES
SQL_CLOSE
SQL_DROP
SQL_UNBIND
SQL_RESET_PARAMS
SQL_FETCH_FIRST
SQL_FETCH_NEXT
SQL_FETCH_LAST
SQL_FETCH_PRIOR
SQL_FETCH_ABSOLUTE
SQL_FETCH_RELATIVE
SQL_COMMIT
SQL_ROLLBACK
SQLTables_TABLE_CATALOG
SQLTables_TABLE_SCHEM
SQLTables_TABLE_NAME
SQLTables_TABLE_TYPE
SQLTables_REMARKS
SQLColumns_TABLE_CAT
SQLColumns_TABLE_SCHEM
SQLColumns_TABLE_NAME
SQLColumns_COLUMN_NAME
SQLColumns_DATA_TYPE
SQLColumns_TYPE_NAME
SQLColumns_COLUMN_SIZE
SQLColumns_BUFFER_LENGTH
SQLColumns_DECIMAL_DIGITS
SQLColumns_NUM_PREC_RADIX
SQLColumns_NULLABLE
SQLColumns_REMARKS
SQLColumns_COLUMN_DEF
SQLColumns_SQL_DATA_TYPE
SQLColumns_SQL_DATETIME_SUB
SQLColumns_CHAR_OCTET_LENGTH
SQLColumns_ORDINAL_POSITION
SQLColumns_IS_NULLABLE