We highly recommend the native database system in NPL: TableDatabase. It is way faster and built-in with NPL runtime.
Using MySql Client¶
- On linux platform, mysql client is by default installed when you build NPLRuntime from source code. You should find a
libluasql.soin NPL runtime directory.
- On windows platform, you must manually install the mysql client connector plugin.
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.
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);