Ring/Lessons/MySQL Functions

MySQL Functions
In this chapter we are going to learn about the MySQL functions provided by the Ring programming language. Before using the functions load the mysqllib.ring library:


 * MySQL_Info
 * MySQL_Init
 * MySQL_Error
 * MySQL_Connect
 * MySQL_Close
 * MySQL_Query
 * MySQL_Insert_ID
 * MySQL_Result
 * MySQL_Next_Result
 * MySQL_Columns
 * MySQL_Result2
 * MySQL_Escape_String
 * MySQL_AutoCommit
 * MySQL_Commit
 * MySQL_Rollback

MySQL_Info Function
We can get the MySQL Client version using the MySQL_Info function.

Syntax:

Example:

Output:

MySQL_Init Function
We can start using MySQL Client through the MySQL_Init function.

Syntax:

MySQL_Error Function
We can get the error message from the MySQL Client using the MySQL_Error function.

Syntax:

MySQL_Connect Function
We can connect to the MySQL database server using the MySQL_Connect function.

Syntax:

MySQL_Close Function
We can close the connection to the MySQL database using the MySQL_Close function

Syntax:

MySQL_Query Function
We can execute SQL queries using the MySQL_Query function

Syntax:

Create Database
The next example connect to MySQL Server then create new database.

Output:

Create Table and Insert Data
The next example create new table and insert records

Output:

MySQL_Insert_ID Function
We can get the inserted row id using the MySQL_Insert_ID function

Syntax:

Example:

see "inserted row id : " + mysql_insert_id(con) + nl	see "close database" + nl	mysql_close(con)

Output:

MySQL_Result Function
We can get the query result (data without column names) using the MySQL_Result function.

Syntax:

MySQL_Next_Result Function
We can move to the next query result using the MySQL_Next_Result function. We use this function when we have multiple SQL statements in the same query.

Syntax:

Print Query Result
The next example execute a query on the database then print the result.

Output:

MySQL_Columns Function
We can get a list of columns names using the MySQL_Columns function.

Syntax:

Example:

Output:

MySQL_Result2 Function
Instead of using MySQL_Result to get the result data without columns names, we can use the MySQL_Result2 to get all of the column names then the query result in one list.

Syntax:

Example:

Output:

MySQL_Escape_String Function
We can store binary data and special characters in the database after processing using MySQL_Escape_String function

Syntax:

Save Image inside the database
Example:

Output:

Restore Image From The Database
Example:

Output:

MySQL_AutoCommit Function
We can enable or disable the auto commit feature using the MySQL_AutoCommit function.

Syntax:

MySQL_Commit Function
We can commit updates to the database using the MySQL_Commit function.

Syntax:

MySQL_Rollback Function
We can rollback updates to the database using the MySQL_Rollback function.

Syntax:

Transaction Example
The next example presents the usage of MySQL_Autocommit, MySQL_Commit & MySQL_RollBack functions.

Example:

Output: