|
|
|
Modula-2 MySQL InterfaceFor ISO Modula-2By Frank Schoonjans (frank.schoonjans@ugent.be)
Using the Modula-2 MySQL library, applications developed in Modula-2 can connect to a MySQL server and use the functions defined in the MySQL API (Application Programming Interface) to build powerfull database applications. These can, but need not, be web applications. In order to use MySQL, the MySQL server must be installed on your system. If it is not, you can download it from the MySQL AB website.
Modula-2 MySQL downloadStony Brook project (Windows) mysql.zip. Contains a Modula-2 interface for libmySQL.dll, the client library that ships with the MySQL database server (libmySQL.dll is also included in the zip file).
All software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of the software described on or downloaded from this website.
Modula-2 MySQL Sample applicationThis simple sample application illustrates the use of the MySQL API. The sample application is included in the download. It requires that you have installed MySQL on your system. Connecting to the MySQL serverBefore we can execute any of the other API functions,
we must establish a connection to the MySQL database engine,
by using the
PROCEDURE InitMySQL(VAR mysql : MYSQL;
host,user,password,dbase : ARRAY OF CHAR;
port: CARDINAL; unix_socket: ARRAY OF CHAR;
clientflag: CARDINAL) : BOOLEAN;
BEGIN
WriteString('Connecting to MySQL ... '); WriteLn;
mysql_init(mysql);
IF mysql_real_connect(mysql,host,user,password,dbase,
port,unix_socket,clientflag) = NIL THEN
Error(mysql,"InitMySQL");
RETURN FALSE;
END;
(* ... *)
RETURN TRUE;
END InitMySQL;
When the function
PROCEDURE Error(mysql: MYSQL; errstr : ARRAY OF CHAR);
BEGIN
WriteString("MySQL error - err#");
WriteCard(mysql_errno(mysql),1); WriteString(" - ");
WriteString(errstr); WriteString(": "); WriteLn;
WriteStrP(mysql_error(mysql));
WriteLn;
END Error;
The
PROCEDURE WriteStrP(P : PCHAR);
TYPE StrP = POINTER TO ARRAY [0..1023] OF CHAR;
BEGIN
WriteString(P:StrP^);
END WriteStrP;
Create databaseAfter we have established a connection to the server, we can create a database using
the following MySQL query: The query is executed by means of the
CONST
Database = "testDB";
PROCEDURE CreateDataBase(VAR mysql : MYSQL; database : ARRAY OF CHAR): BOOLEAN;
VAR query : ARRAY [0..255] OF CHAR;
BEGIN
query := "CREATE DATABASE ";
Append(database,query);
IF mysql_query(mysql,query)=0 THEN
WriteString("Create Database "); WriteString(database);
WriteString(" OK"); WriteLn;
RETURN TRUE;
ELSE
Error(mysql,"Create DataBase");
RETURN FALSE;
END;
END CreateDataBase;
Select databaseNext, we use the procedure
PROCEDURE SelectDataBase(VAR mysql : MYSQL; database : ARRAY OF CHAR): BOOLEAN;
BEGIN
IF mysql_select_db(mysql,database) = 0 THEN
WriteString("Select Database "); WriteString(database);
WriteString(" OK"); WriteLn;
RETURN TRUE;
ELSE
Error(mysql,"Select Database");
RETURN FALSE
END;
END SelectDataBase;
Create tableWe now must create a table in the database by sending a create table query to the MySQL engine: In our example program, we use the following procedure to create the table:
PROCEDURE CreateTable(VAR mysql : MYSQL; table : ARRAY OF CHAR;
columns : ARRAY OF CHAR): BOOLEAN;
VAR query : ARRAY [0..255] OF CHAR;
BEGIN
query := "CREATE TABLE ";
Append(table,query);
Append(" ",query);
Append(columns,query);
IF mysql_query(mysql,query)=0 THEN
WriteString("Create table "); WriteString(table);
WriteString(" OK"); WriteLn;
RETURN TRUE;
ELSE
Error(mysql,"Create table");
RETURN FALSE;
END;
END CreateTable;
The procedure is used as follows:
CreateTable(msql,"people","(name VARCHAR(20), age INT, email VARCHAR(30) )")
This will create a table named "people" in the selected database. The table contains 3 columns for name (text), age (integer) and email (text). Adding data to the tableData are added to the table by sending a query to the MySQL engine.
In our example, this query takes the following form: This is illustrated in the following procedure:
PROCEDURE AddData;
VAR name,email,agestr : ARRAY [0..30] OF CHAR;
age : CARDINAL;
query : ARRAY [0..80] OF CHAR;
BEGIN
WriteString("Enter data. Empty Name = end"); WriteLn;
WriteLn;
LOOP
WriteString("Name : "); ReadString(name); SkipLine;
IF name[0]=CHR(0) THEN RETURN END;
WriteString("Age : "); ReadCard(age); SkipLine;
CardToStr(age,agestr);
WriteString("Email : "); ReadString(email); SkipLine;
WriteLn;
query:="INSERT INTO ";
Append(Table,query);
Append(" VALUES (",query);
Append("'",query); Append(name,query); Append("'",query);
Append(",",query);
Append("'",query); Append(agestr,query); Append("'",query);
Append(",",query);
Append("'",query); Append(email,query); Append("'",query);
Append(")",query);
IF mysql_query(msql,query) # 0 THEN
Error(msql,"Add data query failed"); WriteLn;
END;
END;
END AddData;
Retrieving information from the databaseFirst we illustrate how we can list all data from a table in the database,
by means of the query
PROCEDURE ListAll;
VAR query : ARRAY [0..80] OF CHAR;
BEGIN
query:="SELECT * FROM "+Table;
List(query);
END ListAll;
This procedure simply creates the query string and calls our procedure List:
PROCEDURE List(query : ARRAY OF CHAR);
VAR recbuf : PMYSQL_RES;
rowbuf : MYSQL_ROW;
BEGIN
IF mysql_query(msql,query) # 0 THEN
Error(msql,"Query failed");
RETURN;
END;
WriteLn;
recbuf := mysql_store_result(msql);
IF recbuf=NIL THEN
WriteString('Query returned nil result.'); WriteLn;
RETURN
END;
WriteString('Number of records returned : ');
WriteCard(mysql_num_rows(recbuf),0); WriteLn;
WriteString('Number of fields per record : ');
WriteCard(mysql_num_fields(recbuf),0); WriteLn;
WriteLn;
rowbuf := mysql_fetch_row(recbuf);
WHILE (rowbuf <>NIL) DO
WriteString('Name: '); WriteString(rowbuf^[0]^); WriteString(" - ");
WriteString('Age: '); WriteString(rowbuf^[1]^); WriteString(" - ");
WriteString('Email: '); WriteString(rowbuf^[2]^);
WriteLn;
rowbuf := mysql_fetch_row(recbuf);
END;
WriteLn;
WriteString('Freeing memory occupied by result set...'); WriteLn;
mysql_free_result(recbuf);
END List;
First the query is send to the MySQL engine.
If the Next we retrieve the rows from the database table by the function
rowbuf := mysql_fetch_row(recbuf);
WHILE (rowbuf <>NIL) DO
WriteString('Name: '); WriteString(rowbuf^[0]^); WriteString(" - ");
WriteString('Age: '); WriteString(rowbuf^[1]^); WriteString(" - ");
WriteString('Email: '); WriteString(rowbuf^[2]^);
WriteLn;
rowbuf := mysql_fetch_row(recbuf);
END;
Finally we must free the memory allocated to Searching the database tableWe can search the table with a query In the example application we ask for a name and build the query string in the following procedure (which calls the List procedure described above):
PROCEDURE Search;
VAR name : ARRAY [0..30] OF CHAR;
query : ARRAY [0..80] OF CHAR;
BEGIN
WriteString("Enter name : ");
ReadString(name); SkipLine;
query:="SELECT * FROM "+Table+" WHERE name='";
Append(name,query);
Append("'",query);
List(query);
END Search;
Closing the connectionFinally we must close the connection to the MySQL engine.
mysql_close(msql);
MySQL documentationWeb sitesMySQL AB home page Books
ReviewsMySQL 4 review in PC magazine.
|