A script language of step-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:
Return value:
Error:
Close session.
close();
Input: None.
Return value:
Error:
Switch autocommit mode.
autocommit(mode);
Input:
Return value:
Error:
Commit the current transaction.
commit();
Input: None.
Return value:
Error:
Roll back the current transaction.
rollback();
Input: None.
Return value:
Error:
Get error message.
error();
Input: None.
Return value:
Error:
Get error number.
errno();
Input: None.
Return value:
Error:
Execute SQL.
execute(sql);
Input:
Return value:
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, ], ]