A script language of time-sharing scheduling coroutine in single thread
Melang only support MySQL 8.0, because that version supports asynchronous functions.
If you want to activate MySQL APIs, you should install MySQL library and header files before Melang installation.
mysql = Import('mysql');
Here is the Set Mysql:
Mysql {
fd;
connect(host, port, dbname, username, password);
close();
autocommit(mode);
commit();
rollback();
error();
errno();
execute(sql);
};
fd is a memory block of MySQL structure in C. So it’s value is a memory address, and it’s read-only property.
Create a session on MySQL server.
connect(host, port, dbname, username, password);
Input:
host - a host string of MySQL server.port - an integer port number that MySQL server listening.dbname - database name.username - username.password - password.Return value:
true on success, otherwise false.Error:
Close session.
close();
Input: None.
Return value:
nil.Error:
Switch autocommit mode.
autocommit(mode);
Input:
mode - a boolean value. Set true If want to auto commit, otherwise false.Return value:
true on success, otherwise false.Error:
Commit the current transaction.
commit();
Input: None.
Return value:
true on success, otherwise false.Error:
Roll back the current transaction.
rollback();
Input: None.
Return value:
true on success, otherwise false.Error:
Get error message.
error();
Input: None.
Return value:
false returned.Error:
Get error number.
errno();
Input: None.
Return value:
false returned.Error:
Execute SQL.
execute(sql);
Input:
sql - a SQL string.Return value:
true on success, otherwise false.Error:
There is a database named test, and there is a table people in it:
CREATE TABLE `people` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`age` tinyint unsigned NOT NULL DEFAULT '0',
`name` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Our program:
mysql = Import('mysql');
sys = Import('sys');
m = $mysql; // or m = $Mysql; both are the same. the value of mysql is 'Mysql'.
m.connect('127.0.0.1', 3306, 'test', 'root', '.../*password*/');
sys.print(m.execute('insert into `people` (name, age) values("Tom", 18)'));
result = m.execute('select `name`, `age` from `people`');
sys.print(result);
m.close();
The output is:
true
[[Tom, 18, ], ]