Extension - ODBC

This section describes functions which access relational databases using ODBC (Open Database Connectivity). ODBC is an architecture which isolates applications from databases; it permits multiple connections to multiple databases, possibly from different vendors. It is based on SQL (Structured Query Language), a language whose purpose is to issue queries and transactions to relational databases. Databases which support SQL and ODBC include Oracle(R) and Microsoft Access(R) (commercial), MySQL(R) (commercial or GPL), and Postgresql (open source).

This documentation assumes you have a basic knowledge of SQL. Even if you do not, the examples should help you to get started. On the client computer, you should have ODBC installed (this is the case on Windows computers), and a working database with enough access authorization to connect and issue at least queries.

odbcautocommit

Get or set autocommit mode.

Syntax

b = odbcautocommit(c)
odbcautocommit(c, b)

Description

autocommit(c,b) enables the autocommit mode of connection c if b is true, or disables it is false. By default, autocommit is enabled.

With a single input argument, odbcautocommit gives the current autocommit mode as a boolean value.

When autocommit is enabled, SQL transactions which modify the database, such as insert, update and delete, are committed immediately. When it is disabled, they must be committed explicitly with odbccommit, or canceled with odbcrollback.

ODBC calls

SQLSetConnectAttr, SQLGetConnectAttr

See also

odbccommit, odbcrollback

odbccommit

Commit changes.

Syntax

odbccommit(c)
odbccommit

Description

odbccommit(c) commits last changes made to connection c in an atomic operation. It is not required if autocommit mode is enabled.

Without input argument, odbccommit commits changes of all connections.

ODBC call

SQLEndTran

See also

odbcrollback, odbcautocommit

odbcconnect

Connect to a database.

Syntax

c = odbcconnect(database)
c = odbcconnect(database, username)
c = odbcconnect(database, username, password)

Description

odbcconnect(database,username,password) establishes a connection to a database specified by a DSN (Data Source Name). All input arguments are strings. If the username and password are omitted, the values specified in ODBC for the DSN are used. The result is an identifier which should be used in all other ODBC calls. The connection is closed with odbcdisconnect.

Example

c = odbcconnect('test')
  c =
    0
f = odbcexecdirect(c, 'select * from person;')
  f =
    0
count = odbcrowcount(f)
  count =
    17
numresultcols = odbcnumresultcols(f)
  numresultcols =
    3
list = odbcfieldname(f)
  list =
    {'name','birthdate','country'}
fieldname1 = odbcfieldname(f, 1)
  fieldname1 =
    name
odbcfetch(f);
row = odbcrow(f)
  row =
    {'Joe', [1949,3,24], 'UK'}
odbcfetch(f,'last');
row_field1 = odbcrow(f, 1)
  row_field1 =
    Bob
odbcfreestmt(f);
odbcdisconnect(c);

ODBC calls

SQLAllocHandle, SQLSetEnvAttr, SQLSetConnectAttr, SQLGetDiagField

See also

odbcdisconnect

odbcdisconnect

Disconnect from a database.

Syntax

odbcdisconnect(c)

Description

odbcdisconnect(c) closes the connection c.

ODBC calls

SQLDisconnect, SQLFreeHandle

See also

odbcconnect

odbcexecdirect

Execute an SQL statement.

Syntax

odbcexecdirect(c, cmd)
s = odbcexecdirect(c, cmd)

Description

odbcexecdirect(c,cmd) executes the SQL statement given by the string cmd, using connection c. With an output argument, it keeps the data structures necessary to retrieve data, for instance the table which results from a SELECT statement, until a call to odbcfreestmt.

ODBC calls

SQLExecDirect, SQLAllocHandle, SQLFreeHandle

See also

odbcfetch, odbcgetnumtable, odbcfreestmt

odbcfetch

Fetch a row in the result of a query.

Syntax

odbcfetch(s)
odbcfetch(s, mode)
odbcfetch(s, mode, i)

Description

odbcfetch(s) fetches the next row in the result of odbcexedirect identified by s. Values can then be retrieved with odbcrow.

odbcfetch(s,mode) fetches a row according to string mode ('absolute' and 'relative' require a scalar integer as third input argument):

ModeMeaning
'next'next row
'prior'previous row
'first'first row
'last'last row
'absolute'row i (first=1)
'relative'advance by i (next=1)

ODBC call

SQLFetchScroll

See also

odbcexecdirect, odbcrow, odbcgetnumtable

odbcfielddescr

Get the description of a column.

Syntax

strct = odbcfielddescr(s, i)
list = odbcfielddescr(s)

Description

odbcfielddescr(s,i) gets the description of column i (first=1) for the statement result s. The result is a structure with the following fields:

FieldValue
namecolumn name
datatypedata type
sizedata size
decimaldigitsnumber of decimal digits
nullabletrue if can be the null value

Data type is one of the following strings: 'CHAR', 'VARCHAR', 'LONGVARCHAR', 'WCHAR', 'WVARCHAR', 'WLONGVARCHAR', 'DECIMAL', 'NUMERIC', 'SMALLINT', 'INTEGER', 'REAL', 'FLOAT', 'DOUBLE', 'BIT', 'TINYBIT', 'BIGINT', 'BINARY', 'VARBINARY', 'LONGVARBINARY', 'DATE', 'TIME', 'TIMESTAMP', 'INTERVAL MONTH', 'INTERVAL YEAR', 'INTERVAL YEAR TO MONTH', 'INTERVAL DAY', 'INTERVAL HOUR', 'INTERVAL MINUTE', 'INTERVAL SECOND', 'INTERVAL DAY TO HOUR', 'INTERVAL DAY TO MINUTE', 'INTERVAL DAY TO SECOND', 'INTERVAL HOUR TO MINUTE', 'INTERVAL HOUR TO SECOND', 'INTERVAL MINUTE TO SECOND', 'GUID'.

odbcfielddescr(s) gets the description of all columns as a list of structures.

ODBC calls

SQLDescribeCol, SQLNumResultCols

See also

odbcfieldname, odbcexecdirect

odbcfieldname

Get the name of a column.

Syntax

name = odbcfieldname(s, i)
list = odbcfieldname(s)

Description

odbcfieldname(s,i) gets the name of column i (first=1) for the statement result s. The result is a string.

odbcfieldname(s) gets the names of all columns as a list of strings.

ODBC calls

SQLDescribeCol, SQLNumResultCols

See also

odbcfielddescr, odbcexecdirect

odbcfreestmt

Free the data structure allocated for a statement.

Syntax

odbcfreestmt(s)

Description

odbcfreestmt(s) releases the data structures retained by odbcexecdirect. Afterwards, s is invalid.

ODBC call

SQLFreeHandle

See also

odbcexecdirect

odbcgetnumtable

Get numeric columns as a matrix.

Syntax

matrix = odbcgetnumtable(s, columns)

Description

odbcgetnumtable(s,columns) gets the value of the numeric columns specified in vector columns (1=first). All rows are fetched. The result is a double array.

ODBC calls

SQLNumResultCols, SQLColAttributes, SQLFetchScroll, SQLSetPos, SQLGetData

See also

odbcfetch, odbcrow

odbcnumresultcols

Number of columns in a result.

Syntax

n = odbcnumresultcols(s)

Description

odbcnumresultcols(s) gives the number of columns in result s.

ODBC call

SQLNumResultCols

See also

odbcexecdirect

odbcrollback

Cancel changes.

Syntax

odbcrollback(c)
odbcrollback

Description

In non-autocommit mode, odbcrollback(c) cancels all the changes made with connection c since the previous odbccommit or odbcrollback command. In autocommit mode, it has no effect.

ODBC call

SQLEndTran

See also

odbccommit, odbcautocommit

odbcrow

Get the data of a fetched row.

Syntax

data = odbcrow(s, i)
list = odbcrow(s)

Description

odbcrow(s,i) gets the value of column i (1=first) of the last row fetched for result s. The result is a character string, a real number, or a row vector of real numbers for date or time values.

odbcrow(s) gets the values of all columns in a list.

ODBC calls

SQLNumResultCols, SQLColAttributes, SQLSetPos, SQLGetData

See also

odbcfetch, odbcgetnumtable

odbcrowcount

Number of rows.

Syntax

n = odbcrowcount(s)

Description

odbcrowcount(s) gives the number of rows in result s.

ODBC call

SQLRowCount

See also

odbcexecdirect, odbcfetch, odbcgetnumtable

odbctables

Description of the columns as a table.

Syntax

s = odbctables(c)
s = odbctables(c, catalog)
s = odbctables(c, catalog, schema)
s = odbctables(c, catalog, schema, name)
s = odbctables(c, catalog, schema, name, type)

Description

odbctables(c) gets the description of the tables defined for connection c. Its result is a statement identifier which can be used by functions odbcfetch, odbcrow, and odbcfreestmt.

With additional input arguments, odbctables(s,catalog,schema,name,type) specifies the catalog name, the schema name, and the list of types. These three arguments are strings whose default value is empty and means "everything". The list of types is a comma-separated list of types in a single string, such as 'TABLE,VIEW'. Permitted types include TABLE, VIEW, SYSTEM TABLE, GLOBAL TEMPORARY, LOCAL TEMPORARY, ALIAS, and SYNONYM.

ODBC call

SQLTables

See also

odbcexecdirect, odbcfreestmt


Copyright 2003-2008, Calerga.
All rights reserved.