Easy Manual Installation and Basic Usage of EMySQL API
Using the EMySQL API – Part 1
Foreword: In this part of the series, I explain how easy it is to use and install the EMySQL API; I also explain how to connect, create and select a database.
By: Chrysanthus Date Published: 28 Jul 2016
Introduction
In this series, I show you how to use the EMySQL API by creating and using a database. The activities are similar to what you will find in the MySQL manual. You should also consider the activities as a test of ease of use and robustness of the API; and have confidence in the API.
Hey, the API is free, but do not forget to read the terms of agreement.
The code samples in this series use the console (Prompt or DOS Window) for output.
In this series, you are assumed to be the root user of the MySQL database server.
Pre-Knowledge
To be able to understand this series, you should have professional knowledge in ECMAScript, professional knowledge in MySQL, and database in general. If you do not have that knowledge, click the relevant links below (bottom of page) to have the knowledge first.
In each series you should read the parts in the order given. For each part, the links and order are in a menu on the (top) left of the page.
The API
The API consists of ECMAScript functions, which you use to access the MySQL server. The functions come in a module. To use the module, all you need to do is to learn how to use the functions (and how to install the module) – you will be surprised how easy it is to install and use the module. The module with other packages form a library.
The link to download the software is given below. The file to download is a zipped directory. You download it and you unzip it. After unzipping, you will see the file, Mysql.js (plus maybe another file) and the directory, Mysql.
Installation is easy: copy the file, Mysql.js (plus maybe another file) and the directory, Mysql, to the Node.js server directory in your computer (you should have installed node.js at this point). That is all! Begin to use the API (functions and variables) if you already know how to use it; if you do not already know how to use the API, then continue learning to the end of this series; after which you can start using it. In my computer, the server directory for Node.js is c:/server.
The link to download the file is:
EMySQL API
You may have to scroll down the page of the link before you click the actual download hyperlink.
Basic Usage of the API
Connecting to the MySQL Server
The syntax of the function to connect to the server is:
con = new mysql.Connection("root", "secret", "localhost", 3306, callbackFn(){});
The default MySQL port is 3306. If you are not using this default port, then change it. The callback function definition here, is for handling connection error – see below.
con is an object created that you use with the EMySQL API functions (methods).
Before you could even type the above statement and the rest of the statements below, you should type this at the top of the script:
const mysql = require('./server/Mysql.js');
where the directory, server is the working directory.
You can select a new database to use with the function, whose syntax is:
con.selectDB("databaseName", callbackFn(){});
callbackFn(){} is to handle any error that could occur in the selection of the database.
Statistics Report
You can tell the server to send back a string containing a brief statistics report. A code example to do this is:
con.stats(function(err, result)
{
if (err)
console.log(err);
else
{
console.log(result);
}
});
Here, result is a string. err is false or is a single element array, where the element is a map.
Ping
To know whether the server is alive and reachable, use the ping function. A code example to do this is:
con.ping(function(err, result)
{
if (err)
console.log(err);
else
{
console.log(result);
}
});
Here, result is a string. err is false or a string.
Closing the Connection
To close the connection, you type:
con.close();
The following code gives you the basics on how to code your script. It assumes you already have a MySQL account (root). It also assumes you are using localhost and the default port, 3306 . All examples in this series assume you are using localhost as the domain and the default MySQL port of 3306. The code below uses all the above functions. You will have to type in your own username (root) and password.
const mysql = require('./server/Mysql.js');
con = new mysql.Connection("user", "secret", "localhost", 3306, function(err)
{
if (err)
console.log(err);
});
var db = "test";
con.selectDB(db, function(err)
{
if (err)
console.log(err);
});
con.stats(function(err, result)
{
if (err)
console.log(err);
else
{
console.log(result);
}
});
con.ping(function(err, result)
{
if (err)
console.log(err);
else
{
console.log(result);
}
});
con.close();
I tried it in my computer, there was no problem, and I had the following feedback:
Uptime: 2660 Threads: 3 Questions: 5 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 0 Queries per second avg: 0.001
MySQL Server is alive
Now, note that at the top of the code above, you have,
const mysql = require('./server/Mysql.js');
The statement ends with a semicolon. This is an instruction to the computer to include the functions and variables of the Mysql.js module file (API). server is the working directory.
In my computer, the above script is in a file, I named, clientsql.js (in the drive c:\ root directory). To run the script, I typed,
node clientsql.js
and pressed the Enter key.
That is it for this part of the series. I hope you can see how easy it is to install and use the API. Rendezvous in the next part of the series.
Chrys
Related Links
Free Pure ECMAScript MySQL API for Node.jsMore Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course
NEXT