USAGE:
within prog_unit
#loadlib "mysql.so"
DESCRIPTION:
This class is intended to provide a simple access to a MySQL database server
through the prog_unit.
Class CMySQL:
- constructor CMySQL(char *pcDb):
-
Returns instance for connection with the MySQL database named pcDB. It
reads the environment variables MYSQL_HOST, MYSQL_USER and MYSQL_PWD and
tries to establish the connection with the corresponding parameters.
- constructor CMySQL(char *pcHost, char *pcUser, char *pcPasswd, char *pcDb):
-
Tries to establish a connection with the corresponding parameters and
returns instance to connection if successful.
- float execute(char *pcQuery):
- Executes the SQL query pointed to by the
null-terminated string pcQuery. The query must consist of a single SQL
statement. Returns zero if the query was successful. Non-zero if an error
occurred (the error message is then displayed as a nst_warning). The entire
result of the query is stored at the client side. Therefore, huge result sets
may tie up the client by eating up all available memory. The result set is
freed with the next call to execute() or when all rows of the result set
were retrieved by subsequent calls to fetch_next().
- float num_rows():
- Returns the number of rows of the last result set.
- float num_fields():
- Returns the number of columns of the last result
set.
- float fetch_next():
- Retrieves the next row of the current result
set. Data elements of the row can be accessed with the functions get_text()
and get_float(). Returns 0 if no error occured. Returns -1 if all rows have
been retrieved already, or if no result set is currently present.
- float get_text(int iColumn, char *pcData):
- Copies the data of iColumn
into the array pointed to by pcData (starting with iColumn=0). Note, that
pcData must point to a previously allocated memory array which must be large
enough to hold the data of column iColumn, otherwise the data will be
truncated.
- float get_float(int iColumn):
- Returns a float value corresponding to the
data of column iColumn.
- void escape(char *pcTo, char *pcFrom):
- Encodes the string in pcFrom to
an escaped SQL string, taking into account the current charset of the
connection, that can be sent to the server in a SQL statement, places the
result in pcTo, and adds a terminating null byte. Characters encoded are NUL
(ASCII 0), \n, \r, \, ", ' and Control-Z
EXAMPLE:
The following example is an illustration, of how the class CMySQL can
be used. It connects to the "imdb" database and retrieves all ids and
names from the "movie" table. The data is copied to the output pins
and the successor operands are executed:
OUT char acName[64];
OUT static iNumber;
class CMySQL mysql("imdb");
mysql.execute("select id, name from movie where year>1990");
iNumRows = mysql.num_rows():
for (i=0; i < iNumRows; i++) {
mysql.fetch_next();
iNumber = mysql.get_float(0);
mysql.get_text(1, acName);
exec_opnds();
}
FILE
aux_mysql.cc