Melang

Logo

A script language of time-sharing scheduling coroutine in single thread

View the Project on GitHub Water-Melon/Melang

MySQL

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.

Import

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

fd is a memory block of MySQL structure in C. So it’s value is a memory address, and it’s read-only property.

connect

Create a session on MySQL server.

connect(host, port, dbname, username, password);

Input:

Return value:

Error:

close

Close session.

close();

Input: None.

Return value:

Error:

autocommit

Switch autocommit mode.

autocommit(mode);

Input:

Return value:

Error:

commit

Commit the current transaction.

commit();

Input: None.

Return value:

Error:

rollback

Roll back the current transaction.

rollback();

Input: None.

Return value:

Error:

error

Get error message.

error();

Input: None.

Return value:

Error:

errno

Get error number.

errno();

Input: None.

Return value:

Error:

execute

Execute SQL.

execute(sql);

Input:

Return value:

Error:

Example

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, ], ]