Extension - Client for MySQL databases

This section describes functions which access MySQL(R) relational databases using the MySQL client library.

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 at least the client part of the MySQL software installed, and a working MySQL database with enough access authorization to connect and issue at least queries.

The following functions are defined.

FunctionPurpose
mysql_affectedrowsNumber of affected rows in the last command
mysql_autocommitSet or reset autocommit mode
mysql_closeClose connection to a MySQL database
mysql_commitCommit changes
mysql_connectConnect to a MySQL server
mysql_createdbCreate a new database
mysql_dataseekSet the next row to be fetched
mysql_fetchfieldGet the description of a column
mysql_fetchrowFetch a row in a query result as a list of fields
mysql_fetchstructFetch a row in a query result as a structure
mysql_freeresultFree the data structure allocated for a result
mysql_getclientinfoGet version of the MySQL client
mysql_gethostinfoGet information about the server host
mysql_getserverinfoGet information about the MySQL server
mysql_infoGet information about the client
mysql_numfieldsNumber of fields
mysql_numrowsNumber of rows
mysql_queryExecute an SQL query
mysql_rollbackCancel changes
mysql_selectdbChange current database

MySQL is a registered trademark of MySQL AB in the United States, the European Union and other countries. The LME part of the LME extension source code has been developed by Calerga Sarl. It is provided as a convenience to users of LME-based products.

Compiling the extension

The extension is provided as both compiled library (ready to use) and source code. If you want, you can compile it and link it with the latest release of the client MySQL library. The steps below show the simplest way to do it.

Check your development tools
Make sure you have the development tools required for compiling the extension. Typically, you need a C compiler chain like gcc. You can get it as free software from GNU.
Get MySQL distribution
Download the latest distribution from the site of MySQL AB Alternatively, you can download the binary release for your platform, provided that it includes the development libraries and include files.
Install MySQL distribution
Follow the instructions provided in MySQL documentation.
Locate the required files
To compile the extension, you will need the following files:
Compile the extension
Change the working directory to the directory where LMEMyExt.c is stored. On Linux, you can compile the extension with the following command, replacing paths in italic with the directories found at the previous step:
gcc -Imysqlinc -Lmysqllib -Iextdevel \
 -shared -o myext.so LMEMyExt.c -lmysqlclient
On other platforms, replace -shared with the option(s) for shared libraries: -bundle on Mac OS X, or -G on Solaris.
Install the extension
For most LME applications, just move or copy the extension (myext.so if you have used the command above) to the directory where LME looks for extensions (usually LMEExt). For Sysquake Remote, you also have to add the following line to the configuration file of Apache (please read Sysquake Remote documentation for more information):
SQRLoadExtension extpath/myext.so
where extpath/myext.so is the absolute path of the extension.
Start or restart the LME application
To check that LME has loaded the extension successfully, check the information line starting with mysql client extension. You can also try to evaluate mysql_getclientinfo, which should display the version of the MySQL client.

Functions

mysql_affectedrows

Number of affected rows in the last command.

Syntax

n = mysql_affectedrows(c)

Description

mysql_affectedrows(c) gives the number of affected rows in the last UPDATE, DELETE, or INSERT command.

MySQL call

mysql_affected_rows

See also

mysql_query

mysql_autocommit

Set or reset autocommit mode.

Syntax

mysql_autocommit(c, b)

Description

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

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 mysql_ccommit, or canceled with mysql_rollback.

MySQL call

mysql_autocommit

Availability

MySQL 4.1 or later.

See also

mysql_commit, mysql_rollback

mysql_close

Close connection to a MySQL database.

Syntax

mysql_close(c)

Description

mysql_close(c) closes the connection c.

MySQL call

mysql_close

See also

mysql_connect

mysql_commit

Commit changes.

Syntax

mysql_commit(c)

Description

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

MySQL call

mysql_commit

Availability

MySQL 4.1 or later.

See also

mysql_rollback, mysql_autocommit

mysql_connect

Connect to a MySQL server.

Syntax

c = mysql_connect(server)
c = mysql_connect(server, username)
c = mysql_connect(server, username, password)

Description

mysql_connect(server,username,password) establishes a connection to a database server. All input arguments are strings. If the username and password are omitted, empty values are used. The result is an identifier which should be used in all other MySQL calls. The connection is closed with mysql_close.

Example

c = mysql_connect('test')
  c =
    0
r = mysql_query(c, 'select * from person;')
  r =
    1
count = mysql_numrows(r)
  count =
    17
nfields = mysql_numfields(r)
  nfields =
    3
field1 = mysql_fetchfield(r)
  field1 =
    name: 'name'
    table: 'person'
    length: 30
    max_length: 12
    type: 'VARCHAR'
    not_null: 0
    primary_key: 0
    unique_key: 0
    multiple_key: 0
    numeric: 0
    unsigned: 0
    blob: 0
    zerofill: 0
    decimals: 0
row = mysql_fetchrow(f)
  row =
    {'Joe', [1949,3,24], 'UK'}
row = mysql_fetchstruct(f)
  row =
    name: 'Marco'
    birthdate: [1967,10,9]
    country: 'Italy'
mysql_freeresult(r);
mysql_close(c);

MySQL calls

mysql_init, mysql_real_connect

See also

mysql_close

mysql_createdb

Create a new database.

Syntax

mysql_createdb(c, db)

Description

mysql_createdb(c,db) creates a new database named db with connection c.

MySQL call

mysql_real_query

See also

mysql_fetchrow, mysql_fetchstruct

mysql_dataseek

Set the next row to be fetched.

Syntax

mysql_dataseek(r, i)

Description

mysql_dataseek(c,i) sets the next row to be fetched by mysql_fetchrow or mysql_fetchstruct for the query result r. The row index i is 0-based (first row for i=0), like fseek.

MySQL call

mysql_data_seek

See also

mysql_fetchrow, mysql_fetchstruct

mysql_fetchfield

Get the description of a column.

Syntax

strct = mysql_fetchfield(r, i)
list = mysql_fetchfield(r)

Description

mysql_fetchfield(r,i) gets the description of column i (first=1) for the query result r. The result is a structure with the following fields:

FieldValue
namecolumn name
tabletable name
lengthcolumn width
max_lengthmax width in the current set
typedata type
not_nulltrue if cannot be the null value
primary_keytrue for primary key
unique_keytrue for unique key
multiple_keytrue for multiple key
numerictrue for numeric type
unsignedtrue for unsigned numeric type
blobtrue for blob type
zerofilltrue for zerofill field
decimalsnumber of decimals

Data type is one of the following strings: 'CHAR', 'VARCHAR', 'DECIMAL', 'NUMERIC', 'SMALLINT', 'INTEGER', 'FLOAT', 'DOUBLE', 'BIGINT', 'DATE', 'TIME', 'TIMESTAMP', BLOB, ENUM, SET.

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

MySQL calls

mysql_fetch_fields, mysql_num_fields

See also

mysql_query

mysql_fetchnumtable

Get numeric columns as a matrix.

Syntax

matrix = mysql_fetchnumtable(s, columns)

Description

mysql_fetchnumtable(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.

MySQL calls

(TBD)

See also

mysql_fetchrow

mysql_fetchrow

Fetch a row in the result of a query as a list of fields.

Syntax

list = mysql_fetchrow(r)

Description

mysql_fetchrow(r) fetches the next row in the result of mysql_query identified by r. The result is a list of field values. Numbers are mapped to double real, dates to 1x3 double real matrices [year, month, day], times to 1x3 double real matrices [hour, minute, second], datetime to 1x6 double real matrices [year, month, day, hour, minute, second], blobs to row vectors of type uint8, and everything else to strings.

MySQL calls

mysql_fetch_row, mysql_fetch_lengths

See also

mysql_query, mysql_fetchstruct

mysql_fetchstruct

Fetch a row in the result of a query as a structure.

Syntax

struct = mysql_fetchstruct(r)

Description

mysql_fetchstruct(r) fetches the next row in the result of mysql_query identified by r. The result is a structure whose fields correspond to the columns of the query result.

MySQL calls

mysql_fetch_row, mysql_fetch_lengths, mysql_fetch_fields

See also

mysql_query, mysql_fetchrow

mysql_freeresult

Free the data structure allocated for a result.

Syntax

mysql_freeresult(r)

Description

mysql_freeresult(r) releases the data structures retained by mysql_query. Afterwards, r is invalid.

MySQL call

mysql_free_result

See also

mysql_query

mysql_getclientinfo

Get information about the MySQL client.

Syntax

str = mysql_getclientinfo

Description

mysql_getclientinfo gives the version of the MySQL client. No connection is required.

MySQL call

mysql_get_client_info

See also

mysql_info, mysql_getserverinfo, mysql_gethostinfo

mysql_gethostinfo

Get information about the server host.

Syntax

str = mysql_gethostinfo(c)

Description

mysql_gethostinfo(c) gives information about the server host and the connection type for connection c.

MySQL call

mysql_get_host_info

See also

mysql_getserverinfo, mysql_getclientinfo, mysql_info

mysql_getserverinfo

Get information about the MySQL server.

Syntax

str = mysql_getserverinfo(c)

Description

mysql_getserverinfo(c) gives information about the MySQL server for connection c.

MySQL call

None; definitions MYSQL_CHARSET and MYSQL_VERSION_ID.

See also

mysql_getclientinfo, mysql_info, mysql_gethostinfo

mysql_info

Information about the MySQL client.

Syntax

str = mysql_info

Description

mysql_info gives information about the MySQL client in a string. No connection is required. The information contains the client version, the character set used, and a trademark acknowledgment.

MySQL call

mysql_get_client_info

See also

mysql_getserverinfo, mysql_gethostinfo

mysql_numfields

Number of fields.

Syntax

n = mysql_numfields(r)

Description

mysql_numfields(r) gives the number of fields (columns) in result r.

MySQL call

mysql_num_fields

See also

mysql_query, mysql_numrows

mysql_numrows

Number of rows.

Syntax

n = mysql_numrows(r)

Description

mysql_numrows(r) gives the number of rows in result r.

MySQL call

mysql_num_rows

See also

mysql_query, mysql_numfields

mysql_query

Execute an SQL query.

Syntax

mysql_query(c, cmd)
r = mysql_query(c, cmd)

Description

mysql_query(c,cmd) executes the SQL query 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 mysql_freeresult.

MySQL calls

mysql_real_query, mysql_store_result

See also

mysql_fetchrow, mysql_fetchstruct, mysql_freeresult

mysql_rollback

Cancel changes.

Syntax

mysql_rollback(c)

Description

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

MySQL call

mysql_rollback

Availability

MySQL 4.1 or later.

See also

mysql_commit, mysql_autocommit

mysql_selectdb

Change current database.

Syntax

mysql_selectdb(c, db)

Description

mysql_selectdb(c,db) selects db as the current database for connection c. db is given as a string. All queries which follow are addressed to this database.

MySQL call

mysql_select_db

See also

mysql_connect, mysql_autocommit


Copyright 2004-2008, Calerga.
All rights reserved.