Skip to content
Li, Xizhi edited this page Jul 25, 2017 · 8 revisions

We highly recommend the native database system in NPL: TableDatabase. It is way faster and built-in with NPL runtime.

Using MySql Client

Install Guide

Install On Linux

Before building NPLRuntime from source code, make sure you installed mysql-client and libmysqlclient-dev.

apt-get install mysql-client 
apt-get install libmysqlclient-dev

If you do not have these, the build program will skip mysql support.

On linux platform, mysql client is by default installed when you build NPLRuntime from source code(providing that you already installed mysql-client and libmysqlclient-dev). You should find a luasql.so in NPL runtime directory. Please copy luasql.so to your working directory where your program start, this will allow NPL to automatically find this plugin.

Install On Windows

On windows platform, you must manually build and install the mysql client connector plugin and copy luasql.dll to working directory where your program start.

https://github.com/LiXizhi/luasql

Example Usage

Once installed, you can use it like below:

NPL.load("(gl)script/ide/mysql/mysql.lua");
local MySql = commonlib.gettable("System.Database.MySql");
local mysql_db = MySql:new():init(db_user, db_password, db_name, db_host, db_port);

A Better Way to Code

It is recommended that you write a wrapper file like my_db.page which exposes a global object such as my_db that contains functions to access to your actual database.

An example in the Admin web site framework is here.

  • script/apps/WebServer/admin/wp-includes/wp-db.page

It will manage connection strings (passwords, ips, pools) from global configuration files, etc. In all other server pages, you simply include the file, and call its functions like this

local query = string.format("select * from wp_users where %s = '%s' ",db_field,value);
local user = wpdb:get_row(query);

Using PostgreSQL

PostgreSQL is by default included in NPLRuntime's luasql plugin if it is found in your build computer. The install method and usage is same as MySQL. please refer to https://github.com/LiXizhi/NPLRuntime/blob/master/Server/trunk/luasql/CMakeLists.txt for details

Clone this wiki locally