SlideShare a Scribd company logo
CHAPTER
THIRTYONE
MYSQL FUNCTIONS
In this chapter we are going to learn about the MySQL functions provided by the Ring programming language.
• 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()
31.1 MySQL_Info() Function
We can get the MySQL Client version using the MySQL_Info() function.
Syntax:
MySQL_Info() ---> string contains the MySQL Client version
Example:
see "MySQL Client Version : " + mysql_info()
Output:
MySQL Client Version : 6.1.5
150
Ring Documentation, Release 1.2
31.2 MySQL_Init() Function
We can start using MySQL Client through the MySQL_Init() function.
Syntax:
MySQL_Init() ---> MySQL Handle
31.3 MySQL_Error() Function
We can get the error message from the MySQL Client using the MySQL_Error() function.
Syntax:
MySQL_Error(MySQL Handle) ---> Error message as string
31.4 MySQL_Connect() Function
We can connect to the MySQL database server using the MySQL_Connect() function.
Syntax:
MySQL_Connect(MySQL Handle, cServer, cUserName, cPassword) ---> lStatus
31.5 MySQL_Close() Function
We can close the connection to the MySQL database using the MySQL_Close() function
Syntax:
MySQL_Close(MySQL Handle)
31.6 MySQL_Query() Function
We can execute SQL queries using the MySQL_Query() function
Syntax:
MySQL_Query(MySQL Handle, cSQLQuery)
31.7 Create Database
The next example connect to MySQL Server then create new database.
See "MySQL Test - Create Database" + nl
con = mysql_init()
See "Connect" + nl
31.2. MySQL_Init() Function 151
Ring Documentation, Release 1.2
if mysql_connect(con,"localhost","root","root") = 0
see "Cann't connect" + nl
see "Error : " + mysql_error(con) + nl
mysql_close(con)
bye
ok
See "Create Database..." + nl
mysql_query(con,"CREATE DATABASE mahdb")
See "Close Connection" + nl
mysql_close(con)
Output:
MySQL Test - Create Database
Connect
Create Database...
Close Connection
31.8 Create Table and Insert Data
The next example create new table and insert records
func main
see "Create Table and Insert Records" + nl
con = mysql_init()
see "Connect" + nl
if mysql_connect(con, "localhost", "root", "root","mahdb") = 0
system_error(con)
ok
see "Drop table" + nl
if mysql_query(con, "DROP TABLE IF EXISTS Employee") system_error(con) ok
see "Create table" + nl
if mysql_query(con, "CREATE TABLE Employee(Id INT, Name TEXT, Salary INT)")
system_error(con) ok
see "Insert data" + nl
if mysql_query(con, "INSERT INTO Employee VALUES(1,'Mahmoud',15000)")
system_error(con) ok
if mysql_query(con, "INSERT INTO Employee VALUES(2,'Samir',16000)")
system_error(con) ok
if mysql_query(con, "INSERT INTO Employee VALUES(3,'Fayed',17000)")
system_error(con) ok
see "Close connection" + nl
mysql_close(con)
func system_error con
see mysql_error(con) mysql_close(con) bye
Output:
31.8. Create Table and Insert Data 152
Ring Documentation, Release 1.2
Create Table and Insert Records
Connect
Drop table
Create table
Insert data
Close connection
31.9 MySQL_Insert_ID() Function
We can get the inserted row id using the MySQL_Insert_ID() function
Syntax:
MySQL_Insert_ID() ---> Inserted row id as number
Example:
con = mysql_init()
see "connect to database" + nl
mysql_connect(con,"localhost","root","root","mahdb")
see "drop table" + nl
mysql_query(con, "DROP TABLE IF EXISTS Customers")
see "create table" + nl
mysql_query(con, "CREATE TABLE Customers(Id INT PRIMARY KEY AUTO_INCREMENT, Name TEXT)")
see "insert record" + nl
mysql_query(con, "INSERT INTO Customers(Name) VALUES('Mahmoud')")
see "insert record" + nl
mysql_query(con, "INSERT INTO Customers(Name) VALUES('Samir')")
see "insert record" + nl
mysql_query(con, "INSERT INTO Customers(Name) VALUES('Fayed')")
see "insert record" + nl
mysql_query(con, "INSERT INTO Customers(Name) VALUES('Test 2015')")
see "inserted row id : " + mysql_insert_id(con) + nl
see "close database" + nl
mysql_close(con)
Output:
connect to database
drop table
create table
insert record
insert record
insert record
insert record
inserted row id : 4
close database
31.10 MySQL_Result() Function
We can get the query result (data without column names) using the MySQL_Result() function.
Syntax:
31.9. MySQL_Insert_ID() Function 153
Ring Documentation, Release 1.2
MySQL_Result(MySQL Handle) ---> List contains the query result
31.11 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:
MySQL_Next_Result(MySQL Handle)
31.12 Print Query Result
The next example execute a query on the database then print the result.
con = mysql_init()
see "Connect to database" + nl
mysql_connect(con, "localhost", "root", "root","mahdb")
see "Execute Query" + nl
mysql_query(con, "SELECT Name FROM Employee WHERE Id=1;"+
"SELECT Name FROM Employee WHERE Id=3")
see "Print Result" + nl
see mysql_result(con)
mysql_next_result(con)
see mysql_result(con)
see "close database" + nl
mysql_close(con)
Output:
Connect to database
Execute Query
Print Result
Mahmoud
Fayed
close database
31.13 MySQL_Columns() Function
We can get a list of columns names using the MySQL_Columns() function.
Syntax:
MySQL_Columns(MySQL Handle) ---> List contains columns information
Example:
con = mysql_init()
see "Connect to database" + nl
mysql_connect(con, "localhost", "root", "root","mahdb")
see "Execute Query" + nl
mysql_query(con, "SELECT * FROM Employee")
see "Result" + nl
31.11. MySQL_Next_Result() Function 154
Ring Documentation, Release 1.2
see mysql_columns(con)
see "Close database" + nl
mysql_close(con)
Output:
Connect to database
Execute Query
Result
Id
11
3
32768
Name
65535
252
16
Salary
11
3
32768
Close database
31.14 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:
MySQL_Result2(MySQL Handle) ---> List (query result starts with columns names)
Example:
con = mysql_init()
see "Connect to database" + nl
mysql_connect(con, "localhost", "root", "root","mahdb")
see "Execute Query" + nl
mysql_query(con, "SELECT * FROM Employee")
see "Print Result" + nl
see mysql_result2(con)
see "Close database" + nl
mysql_close(con)
Output:
Connect to database
Execute Query
Print Result
Id
Name
Salary
1
Mahmoud
15000
2
Samir
31.14. MySQL_Result2() Function 155
Ring Documentation, Release 1.2
16000
3
Fayed
17000
Close database
31.15 MySQL_Escape_String() Function
We can store binary data and special characters in the database after processing using MySQL_Escape_String() func-
tion
Syntax:
MySQL_Escape_String(MySQL Handle, cString) ---> String after processing
31.16 Save Image inside the database
Example:
See "Read file" + nl
cFile = read("testsmahmoud.jpg")
con = mysql_init()
See "Connect to database..." + nl
mysql_connect(con, "localhost", "root", "root","mahdb")
See "Escape string..." + nl
cFile = mysql_escape_string(con,cFile)
stmt = "INSERT INTO photo(id, data) VALUES(1, '" + cFile + "')"
See "Insert data..." + nl
mysql_query(con,stmt)
See "Close database..." + nl
mysql_close(con)
Output:
Read file
Connect to database...
Escape string...
Insert data...
Close database...
31.17 Restore Image From The Database
Example:
con = mysql_init()
See "Connect to database..." + nl
mysql_connect(con, "localhost", "root", "root","mahdb")
See "Read data from database..." + nl
mysql_query(con,"SELECT data FROM photo WHERE id=1")
See "Write new file" + nl
result = mysql_result(con)
write("testsmahmoud2.jpg",result[1][1])
31.15. MySQL_Escape_String() Function 156
Ring Documentation, Release 1.2
See "Close database..." + nl
mysql_close(con)
Output:
Connect to database...
Read data from database...
Write new file
Close database...
31.18 MySQL_AutoCommit() Function
We can enable or disable the auto commit feature using the MySQL_AutoCommit() function.
Syntax:
MySQL_AutoCommit(MySQL Handle, lStatus) # lstatus can be True/False
31.19 MySQL_Commit() Function
We can commit updates to the database using the MySQL_Commit() function.
Syntax:
MySQL_Commit(MySQL Handle)
31.20 MySQL_Rollback() Function
We can rollback updates to the database using the MySQL_Rollback() function.
Syntax:
MySQL_Rollback(MySQL Handle)
31.21 Transaction Example
The next example presents the usage of MySQL_Autocommit(), MySQL_Commit() & MySQL_RollBack() functions.
Example:
func main
con = mysql_init()
see "Connect" + nl
if mysql_connect(con, "localhost", "root", "root","mahdb") = 0
system_error(con) ok
see "Drop table" + nl
if mysql_query(con, "DROP TABLE IF EXISTS Employee2")
system_error(con) ok
31.18. MySQL_AutoCommit() Function 157
Ring Documentation, Release 1.2
see "Create table" + nl
if mysql_query(con, "CREATE TABLE Employee2(Id INT, Name TEXT, Salary INT)")
system_error(con) ok
see "Insert data" + nl
if mysql_query(con, "INSERT INTO Employee2 VALUES(1,'Mahmoud',15000)")
system_error(con) ok
if mysql_query(con, "INSERT INTO Employee2 VALUES(2,'Samir',16000)")
system_error(con) ok
if mysql_query(con, "INSERT INTO Employee2 VALUES(3,'Fayed',17000)")
system_error(con) ok
mysql_autocommit(con,False)
mysql_query(con, "INSERT INTO Employee2 VALUES(4,'Ahmed',5000)")
mysql_query(con, "INSERT INTO Employee2 VALUES(5,'Ibrahim',50000)")
mysql_query(con, "INSERT INTO Employee2 VALUES(6,'Mohammed',50000)")
See "Save transaction (y/n) " give nChoice
if upper(nChoice) = "Y"
mysql_commit(con)
else
mysql_rollback(con)
ok
see "Close connection" + nl
mysql_close(con)
func system_error con
see mysql_error(con)
mysql_close(con)
bye
Output:
Connect
Drop table
Create table
Insert data
Save transaction (y/n) y
Close connection
31.21. Transaction Example 158
CHAPTER
THIRTYTWO
SQLITE FUNCTIONS
In this chapter we will learn about using the SQLite database in the Ring programming language.
32.1 sqlite_init() function
Syntax:
sqlite_init() ---> SQLite Object
32.2 sqlite_open() function
Syntax:
sqlite_open(SQLite Object,cFileName)
32.3 sqlite_execute() function
Syntax:
sqlite_exexute(SQLite Object,cSQLStatement)
32.4 sqlite_close() function
Syntax:
sqlite_close(SQLite Object)
32.5 Example
The next code create a SQLite database, add new records then display the data.
159

More Related Content

PDF
The Ring programming language version 1.3 book - Part 20 of 88
PDF
The Ring programming language version 1.5.2 book - Part 28 of 181
PDF
The Ring programming language version 1.10 book - Part 37 of 212
PDF
The Ring programming language version 1.7 book - Part 32 of 196
PDF
The Ring programming language version 1.5.3 book - Part 29 of 184
PDF
The Ring programming language version 1.5.1 book - Part 27 of 180
PDF
The Ring programming language version 1.5.4 book - Part 29 of 185
PDF
The Ring programming language version 1.8 book - Part 34 of 202
The Ring programming language version 1.3 book - Part 20 of 88
The Ring programming language version 1.5.2 book - Part 28 of 181
The Ring programming language version 1.10 book - Part 37 of 212
The Ring programming language version 1.7 book - Part 32 of 196
The Ring programming language version 1.5.3 book - Part 29 of 184
The Ring programming language version 1.5.1 book - Part 27 of 180
The Ring programming language version 1.5.4 book - Part 29 of 185
The Ring programming language version 1.8 book - Part 34 of 202

What's hot (19)

PDF
The Ring programming language version 1.9 book - Part 36 of 210
PDF
The Ring programming language version 1.6 book - Part 31 of 189
PDF
The Ring programming language version 1.4.1 book - Part 8 of 31
PDF
Elixir and OTP Apps introduction
PPTX
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
PDF
The Ring programming language version 1.5.1 book - Part 42 of 180
PPTX
Sequelize
PPTX
MySql:Introduction
PPTX
MySql:Basics
PPTX
FRP: What does "declarative" mean
PDF
The Ring programming language version 1.2 book - Part 16 of 84
PDF
The Ring programming language version 1.5.2 book - Part 26 of 181
PDF
Store and Process Big Data with Hadoop and Cassandra
PPT
Intro to my sql
PPTX
Django - sql alchemy - jquery
PDF
Data Love Conference - Window Functions for Database Analytics
PDF
The Ring programming language version 1.3 book - Part 7 of 88
DOCX
Punto fijo multivariante
The Ring programming language version 1.9 book - Part 36 of 210
The Ring programming language version 1.6 book - Part 31 of 189
The Ring programming language version 1.4.1 book - Part 8 of 31
Elixir and OTP Apps introduction
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
The Ring programming language version 1.5.1 book - Part 42 of 180
Sequelize
MySql:Introduction
MySql:Basics
FRP: What does "declarative" mean
The Ring programming language version 1.2 book - Part 16 of 84
The Ring programming language version 1.5.2 book - Part 26 of 181
Store and Process Big Data with Hadoop and Cassandra
Intro to my sql
Django - sql alchemy - jquery
Data Love Conference - Window Functions for Database Analytics
The Ring programming language version 1.3 book - Part 7 of 88
Punto fijo multivariante
Ad

Similar to The Ring programming language version 1.2 book - Part 18 of 84 (20)

PDF
The Ring programming language version 1.5.2 book - Part 29 of 181
PDF
The Ring programming language version 1.3 book - Part 83 of 88
PDF
The Ring programming language version 1.5.3 book - Part 54 of 184
PDF
The Ring programming language version 1.5.3 book - Part 44 of 184
PDF
The Ring programming language version 1.4 book - Part 8 of 30
PDF
The Ring programming language version 1.2 book - Part 32 of 84
PPTX
Database Connectivity using Python and MySQL
PDF
The Ring programming language version 1.5.3 book - Part 28 of 184
PDF
The Ring programming language version 1.6 book - Part 46 of 189
PDF
The Ring programming language version 1.2 book - Part 17 of 84
PDF
The Ring programming language version 1.10 book - Part 54 of 212
PDF
The Ring programming language version 1.2 book - Part 79 of 84
PDF
The Ring programming language version 1.2 book - Part 19 of 84
PDF
The Ring programming language version 1.3 book - Part 34 of 88
PDF
The Ring programming language version 1.3 book - Part 19 of 88
PDF
python-mysql connectivity (1).pdf
PDF
The Ring programming language version 1.8 book - Part 43 of 202
PDF
The Ring programming language version 1.6 book - Part 30 of 189
PDF
The Ring programming language version 1.8 book - Part 33 of 202
The Ring programming language version 1.5.2 book - Part 29 of 181
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.5.3 book - Part 54 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.4 book - Part 8 of 30
The Ring programming language version 1.2 book - Part 32 of 84
Database Connectivity using Python and MySQL
The Ring programming language version 1.5.3 book - Part 28 of 184
The Ring programming language version 1.6 book - Part 46 of 189
The Ring programming language version 1.2 book - Part 17 of 84
The Ring programming language version 1.10 book - Part 54 of 212
The Ring programming language version 1.2 book - Part 79 of 84
The Ring programming language version 1.2 book - Part 19 of 84
The Ring programming language version 1.3 book - Part 34 of 88
The Ring programming language version 1.3 book - Part 19 of 88
python-mysql connectivity (1).pdf
The Ring programming language version 1.8 book - Part 43 of 202
The Ring programming language version 1.6 book - Part 30 of 189
The Ring programming language version 1.8 book - Part 33 of 202
Ad

More from Mahmoud Samir Fayed (20)

PDF
The Ring programming language version 1.10 book - Part 212 of 212
PDF
The Ring programming language version 1.10 book - Part 211 of 212
PDF
The Ring programming language version 1.10 book - Part 210 of 212
PDF
The Ring programming language version 1.10 book - Part 208 of 212
PDF
The Ring programming language version 1.10 book - Part 207 of 212
PDF
The Ring programming language version 1.10 book - Part 205 of 212
PDF
The Ring programming language version 1.10 book - Part 206 of 212
PDF
The Ring programming language version 1.10 book - Part 204 of 212
PDF
The Ring programming language version 1.10 book - Part 203 of 212
PDF
The Ring programming language version 1.10 book - Part 202 of 212
PDF
The Ring programming language version 1.10 book - Part 201 of 212
PDF
The Ring programming language version 1.10 book - Part 200 of 212
PDF
The Ring programming language version 1.10 book - Part 199 of 212
PDF
The Ring programming language version 1.10 book - Part 198 of 212
PDF
The Ring programming language version 1.10 book - Part 197 of 212
PDF
The Ring programming language version 1.10 book - Part 196 of 212
PDF
The Ring programming language version 1.10 book - Part 195 of 212
PDF
The Ring programming language version 1.10 book - Part 194 of 212
PDF
The Ring programming language version 1.10 book - Part 193 of 212
PDF
The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 192 of 212

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Introduction to Artificial Intelligence
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
ai tools demonstartion for schools and inter college
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
medical staffing services at VALiNTRY
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Digital Strategies for Manufacturing Companies
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
Nekopoi APK 2025 free lastest update
Wondershare Filmora 15 Crack With Activation Key [2025
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Introduction to Artificial Intelligence
How Creative Agencies Leverage Project Management Software.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
VVF-Customer-Presentation2025-Ver1.9.pptx
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
ai tools demonstartion for schools and inter college
PTS Company Brochure 2025 (1).pdf.......
medical staffing services at VALiNTRY
Design an Analysis of Algorithms I-SECS-1021-03
Digital Strategies for Manufacturing Companies
Softaken Excel to vCard Converter Software.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
How to Migrate SBCGlobal Email to Yahoo Easily

The Ring programming language version 1.2 book - Part 18 of 84

  • 1. CHAPTER THIRTYONE MYSQL FUNCTIONS In this chapter we are going to learn about the MySQL functions provided by the Ring programming language. • 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() 31.1 MySQL_Info() Function We can get the MySQL Client version using the MySQL_Info() function. Syntax: MySQL_Info() ---> string contains the MySQL Client version Example: see "MySQL Client Version : " + mysql_info() Output: MySQL Client Version : 6.1.5 150
  • 2. Ring Documentation, Release 1.2 31.2 MySQL_Init() Function We can start using MySQL Client through the MySQL_Init() function. Syntax: MySQL_Init() ---> MySQL Handle 31.3 MySQL_Error() Function We can get the error message from the MySQL Client using the MySQL_Error() function. Syntax: MySQL_Error(MySQL Handle) ---> Error message as string 31.4 MySQL_Connect() Function We can connect to the MySQL database server using the MySQL_Connect() function. Syntax: MySQL_Connect(MySQL Handle, cServer, cUserName, cPassword) ---> lStatus 31.5 MySQL_Close() Function We can close the connection to the MySQL database using the MySQL_Close() function Syntax: MySQL_Close(MySQL Handle) 31.6 MySQL_Query() Function We can execute SQL queries using the MySQL_Query() function Syntax: MySQL_Query(MySQL Handle, cSQLQuery) 31.7 Create Database The next example connect to MySQL Server then create new database. See "MySQL Test - Create Database" + nl con = mysql_init() See "Connect" + nl 31.2. MySQL_Init() Function 151
  • 3. Ring Documentation, Release 1.2 if mysql_connect(con,"localhost","root","root") = 0 see "Cann't connect" + nl see "Error : " + mysql_error(con) + nl mysql_close(con) bye ok See "Create Database..." + nl mysql_query(con,"CREATE DATABASE mahdb") See "Close Connection" + nl mysql_close(con) Output: MySQL Test - Create Database Connect Create Database... Close Connection 31.8 Create Table and Insert Data The next example create new table and insert records func main see "Create Table and Insert Records" + nl con = mysql_init() see "Connect" + nl if mysql_connect(con, "localhost", "root", "root","mahdb") = 0 system_error(con) ok see "Drop table" + nl if mysql_query(con, "DROP TABLE IF EXISTS Employee") system_error(con) ok see "Create table" + nl if mysql_query(con, "CREATE TABLE Employee(Id INT, Name TEXT, Salary INT)") system_error(con) ok see "Insert data" + nl if mysql_query(con, "INSERT INTO Employee VALUES(1,'Mahmoud',15000)") system_error(con) ok if mysql_query(con, "INSERT INTO Employee VALUES(2,'Samir',16000)") system_error(con) ok if mysql_query(con, "INSERT INTO Employee VALUES(3,'Fayed',17000)") system_error(con) ok see "Close connection" + nl mysql_close(con) func system_error con see mysql_error(con) mysql_close(con) bye Output: 31.8. Create Table and Insert Data 152
  • 4. Ring Documentation, Release 1.2 Create Table and Insert Records Connect Drop table Create table Insert data Close connection 31.9 MySQL_Insert_ID() Function We can get the inserted row id using the MySQL_Insert_ID() function Syntax: MySQL_Insert_ID() ---> Inserted row id as number Example: con = mysql_init() see "connect to database" + nl mysql_connect(con,"localhost","root","root","mahdb") see "drop table" + nl mysql_query(con, "DROP TABLE IF EXISTS Customers") see "create table" + nl mysql_query(con, "CREATE TABLE Customers(Id INT PRIMARY KEY AUTO_INCREMENT, Name TEXT)") see "insert record" + nl mysql_query(con, "INSERT INTO Customers(Name) VALUES('Mahmoud')") see "insert record" + nl mysql_query(con, "INSERT INTO Customers(Name) VALUES('Samir')") see "insert record" + nl mysql_query(con, "INSERT INTO Customers(Name) VALUES('Fayed')") see "insert record" + nl mysql_query(con, "INSERT INTO Customers(Name) VALUES('Test 2015')") see "inserted row id : " + mysql_insert_id(con) + nl see "close database" + nl mysql_close(con) Output: connect to database drop table create table insert record insert record insert record insert record inserted row id : 4 close database 31.10 MySQL_Result() Function We can get the query result (data without column names) using the MySQL_Result() function. Syntax: 31.9. MySQL_Insert_ID() Function 153
  • 5. Ring Documentation, Release 1.2 MySQL_Result(MySQL Handle) ---> List contains the query result 31.11 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: MySQL_Next_Result(MySQL Handle) 31.12 Print Query Result The next example execute a query on the database then print the result. con = mysql_init() see "Connect to database" + nl mysql_connect(con, "localhost", "root", "root","mahdb") see "Execute Query" + nl mysql_query(con, "SELECT Name FROM Employee WHERE Id=1;"+ "SELECT Name FROM Employee WHERE Id=3") see "Print Result" + nl see mysql_result(con) mysql_next_result(con) see mysql_result(con) see "close database" + nl mysql_close(con) Output: Connect to database Execute Query Print Result Mahmoud Fayed close database 31.13 MySQL_Columns() Function We can get a list of columns names using the MySQL_Columns() function. Syntax: MySQL_Columns(MySQL Handle) ---> List contains columns information Example: con = mysql_init() see "Connect to database" + nl mysql_connect(con, "localhost", "root", "root","mahdb") see "Execute Query" + nl mysql_query(con, "SELECT * FROM Employee") see "Result" + nl 31.11. MySQL_Next_Result() Function 154
  • 6. Ring Documentation, Release 1.2 see mysql_columns(con) see "Close database" + nl mysql_close(con) Output: Connect to database Execute Query Result Id 11 3 32768 Name 65535 252 16 Salary 11 3 32768 Close database 31.14 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: MySQL_Result2(MySQL Handle) ---> List (query result starts with columns names) Example: con = mysql_init() see "Connect to database" + nl mysql_connect(con, "localhost", "root", "root","mahdb") see "Execute Query" + nl mysql_query(con, "SELECT * FROM Employee") see "Print Result" + nl see mysql_result2(con) see "Close database" + nl mysql_close(con) Output: Connect to database Execute Query Print Result Id Name Salary 1 Mahmoud 15000 2 Samir 31.14. MySQL_Result2() Function 155
  • 7. Ring Documentation, Release 1.2 16000 3 Fayed 17000 Close database 31.15 MySQL_Escape_String() Function We can store binary data and special characters in the database after processing using MySQL_Escape_String() func- tion Syntax: MySQL_Escape_String(MySQL Handle, cString) ---> String after processing 31.16 Save Image inside the database Example: See "Read file" + nl cFile = read("testsmahmoud.jpg") con = mysql_init() See "Connect to database..." + nl mysql_connect(con, "localhost", "root", "root","mahdb") See "Escape string..." + nl cFile = mysql_escape_string(con,cFile) stmt = "INSERT INTO photo(id, data) VALUES(1, '" + cFile + "')" See "Insert data..." + nl mysql_query(con,stmt) See "Close database..." + nl mysql_close(con) Output: Read file Connect to database... Escape string... Insert data... Close database... 31.17 Restore Image From The Database Example: con = mysql_init() See "Connect to database..." + nl mysql_connect(con, "localhost", "root", "root","mahdb") See "Read data from database..." + nl mysql_query(con,"SELECT data FROM photo WHERE id=1") See "Write new file" + nl result = mysql_result(con) write("testsmahmoud2.jpg",result[1][1]) 31.15. MySQL_Escape_String() Function 156
  • 8. Ring Documentation, Release 1.2 See "Close database..." + nl mysql_close(con) Output: Connect to database... Read data from database... Write new file Close database... 31.18 MySQL_AutoCommit() Function We can enable or disable the auto commit feature using the MySQL_AutoCommit() function. Syntax: MySQL_AutoCommit(MySQL Handle, lStatus) # lstatus can be True/False 31.19 MySQL_Commit() Function We can commit updates to the database using the MySQL_Commit() function. Syntax: MySQL_Commit(MySQL Handle) 31.20 MySQL_Rollback() Function We can rollback updates to the database using the MySQL_Rollback() function. Syntax: MySQL_Rollback(MySQL Handle) 31.21 Transaction Example The next example presents the usage of MySQL_Autocommit(), MySQL_Commit() & MySQL_RollBack() functions. Example: func main con = mysql_init() see "Connect" + nl if mysql_connect(con, "localhost", "root", "root","mahdb") = 0 system_error(con) ok see "Drop table" + nl if mysql_query(con, "DROP TABLE IF EXISTS Employee2") system_error(con) ok 31.18. MySQL_AutoCommit() Function 157
  • 9. Ring Documentation, Release 1.2 see "Create table" + nl if mysql_query(con, "CREATE TABLE Employee2(Id INT, Name TEXT, Salary INT)") system_error(con) ok see "Insert data" + nl if mysql_query(con, "INSERT INTO Employee2 VALUES(1,'Mahmoud',15000)") system_error(con) ok if mysql_query(con, "INSERT INTO Employee2 VALUES(2,'Samir',16000)") system_error(con) ok if mysql_query(con, "INSERT INTO Employee2 VALUES(3,'Fayed',17000)") system_error(con) ok mysql_autocommit(con,False) mysql_query(con, "INSERT INTO Employee2 VALUES(4,'Ahmed',5000)") mysql_query(con, "INSERT INTO Employee2 VALUES(5,'Ibrahim',50000)") mysql_query(con, "INSERT INTO Employee2 VALUES(6,'Mohammed',50000)") See "Save transaction (y/n) " give nChoice if upper(nChoice) = "Y" mysql_commit(con) else mysql_rollback(con) ok see "Close connection" + nl mysql_close(con) func system_error con see mysql_error(con) mysql_close(con) bye Output: Connect Drop table Create table Insert data Save transaction (y/n) y Close connection 31.21. Transaction Example 158
  • 10. CHAPTER THIRTYTWO SQLITE FUNCTIONS In this chapter we will learn about using the SQLite database in the Ring programming language. 32.1 sqlite_init() function Syntax: sqlite_init() ---> SQLite Object 32.2 sqlite_open() function Syntax: sqlite_open(SQLite Object,cFileName) 32.3 sqlite_execute() function Syntax: sqlite_exexute(SQLite Object,cSQLStatement) 32.4 sqlite_close() function Syntax: sqlite_close(SQLite Object) 32.5 Example The next code create a SQLite database, add new records then display the data. 159