環/課程/MySQL 函式
在本章中,我們將學習環程式語言提供的 MySQL 函式。在使用這些函式之前,請載入 mysqllib.ring 庫
load "mysqllib.ring"- 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() 函式獲取 MySQL 客戶端版本。
語法
MySQL_Info() ---> string contains the MySQL Client version示例
see "MySQL Client Version : " + mysql_info()輸出
MySQL Client Version : 6.1.5我們可以透過 MySQL_Init() 函式開始使用 MySQL 客戶端。
語法
MySQL_Init() ---> MySQL Handle我們可以使用 MySQL_Error() 函式從 MySQL 客戶端獲取錯誤訊息。
語法
MySQL_Error(MySQL Handle) ---> Error message as string我們可以使用 MySQL_Connect() 函式連線到 MySQL 資料庫伺服器。
語法
MySQL_Connect(MySQL Handle, cServer, cUserName, cPassword) ---> lStatus我們可以使用 MySQL_Close() 函式關閉與 MySQL 資料庫的連線
語法
MySQL_Close(MySQL Handle)我們可以使用 MySQL_Query() 函式執行 SQL 查詢
語法
MySQL_Query(MySQL Handle, cSQLQuery)下面的示例連線到 MySQL 伺服器,然後建立新的資料庫。
See "MySQL Test - Create Database" + nl
con = mysql_init()
See "Connect" + nl
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)輸出
MySQL Test - Create Database
Connect
Create Database...
Close Connection下面的示例建立新的表格並插入記錄
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輸出
Create Table and Insert Records
Connect
Drop table
Create table
Insert data
Close connection我們可以使用 MySQL_Insert_ID() 函式獲取插入的行 ID
語法
MySQL_Insert_ID() ---> Inserted row id as number示例
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)
輸出
connect to database
drop table
create table
insert record
insert record
insert record
insert record
inserted row id : 4
close database我們可以使用 MySQL_Result() 函式獲取查詢結果(不帶列名的資料)。
語法
MySQL_Result(MySQL Handle) ---> List contains the query result我們可以使用 MySQL_Next_Result() 函式移動到下一個查詢結果。當我們在同一個查詢中有多個 SQL 語句時,我們使用此函式。
語法
MySQL_Next_Result(MySQL Handle)下面的示例在資料庫上執行一個查詢,然後列印結果。
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)輸出
Connect to database
Execute Query
Print Result
Mahmoud
Fayed
close database我們可以使用 MySQL_Columns() 函式獲取列名列表。
語法
MySQL_Columns(MySQL Handle) ---> List contains columns information示例
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
see mysql_columns(con)
see "Close database" + nl
mysql_close(con)輸出
Connect to database
Execute Query
Result
Id
11
3
32768
Name
65535
252
16
Salary
11
3
32768
Close database我們可以使用 MySQL_Result2() 函式獲取所有列名和查詢結果,而不是使用 MySQL_Result() 函式獲取不帶列名的結果資料。
語法
MySQL_Result2(MySQL Handle) ---> List (query result starts with columns names)示例
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)輸出
Connect to database
Execute Query
Print Result
Id
Name
Salary
1
Mahmoud
15000
2
Samir
16000
3
Fayed
17000
Close database
在使用 MySQL_Escape_String() 函式處理後,我們可以將二進位制資料和特殊字元儲存在資料庫中
語法
MySQL_Escape_String(MySQL Handle, cString) ---> String after processing示例
See "Read file" + nl
cFile = read("tests\mahmoud.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)輸出
Read file
Connect to database...
Escape string...
Insert data...
Close database...示例
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("tests\mahmoud2.jpg",result[1][1])
See "Close database..." + nl
mysql_close(con)輸出
Connect to database...
Read data from database...
Write new file
Close database...
我們可以使用 MySQL_AutoCommit() 函式啟用或停用自動提交功能。
語法
MySQL_AutoCommit(MySQL Handle, lStatus) # lstatus can be True/False我們可以使用 MySQL_Commit() 函式提交對資料庫的更新。
語法
MySQL_Commit(MySQL Handle)我們可以使用 MySQL_Rollback() 函式回滾對資料庫的更新。
語法
MySQL_Rollback(MySQL Handle)以下示例演示了 MySQL_Autocommit()、MySQL_Commit() 和 MySQL_RollBack() 函式的使用。
示例
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
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輸出
Connect
Drop table
Create table
Insert data
Save transaction (y/n) y
Close connection