EMySQL API
Free Pure ECMAScript MySQL API to Download
Pure ECMAScript MySQL Application Programming Interface for Node.js
Foreword: On this page you have the features of the API and code examples on how to use them; you also have the download hyperlink.
By: Chrysanthus Date Published: 28 Jul 2016
EMySQL API for all Operating Systems
Security
Important politicians' email can be hacked; until you now have ransome-ware. So, you can no longer afford to take any risk. Block it!
You might have opened a social network page and saw information that was not meant for you. That is a big opening for hacking. The MySQL protocol does not provide any mechanism to prevent two or more people from sharing the same connection.
However, the free EMySQL API stops two or more people from sharing the same connection. Now, block the opening (insecurity) by downloading the free EMySQL API (below).
Using the API
Including
const mysql = require('./server/Mysql.js');
where Mysql.js is the main module file and it is in the directory, server.
Connection
var con = new mysql.Connection("user", "secret", "localhost", 3306, function(err)
{
if (err)
console.log(err);
});
In the API, err is either false, a string or a single element array, whose element is a map with error data.
Query
var selStr = `SELECT * FROM pet WHERE species = 'snake' OR species = 'bird'`;
con.query(selStr, function(err, result)
{
if (err)
console.log(err);
else
{
for (i=0; i<result.length; ++i)
{
tempStr = `${result[i].get('name')}, ${result[i].get('owner')}, ${result[i].get('species')}, ${result[i].get('sex')}, ${result[i].get('birth')}, ${result[i].get('death')}`
console.log(tempStr);
}
}
});
Note the use of backticks for query string.
var db = "test";
con.selectDB(db, function(err)
{
if (err)
console.log(err);
});
Statistics Report
con.stats(function(err, result)
{
if (err)
console.log(err);
else
{
console.log(result);
}
});
Ping
con.ping(function(err, result)
{
if (err)
console.log(err);
else
{
console.log(result);
}
});
Closing the Connection
con.close();
You should close the connection.
Prepare and Execute
var prepareStr = `INSERT INTO pet (name, owner, species, sex, birth, death) VALUES ('Nelly',?,?,'m','2009-03-30',NULL)`;
con.prepare(prepareStr, function(err, feedback)
{
if (err)
console.log(err);
else
console.log('Statement has been prepared');
});
var executeStr = `'Marie','wild'`;
con.execute(executeStr, function(err, feedback)
{
if (err)
console.log(err);
else
console.log('Row has been inserted');
});
con.statementClose();
Long Cell Values
var prepareStr = `INSERT INTO pet (name, owner, species, sex, birth, death) VALUES ('Piggy',?,?,'m','2009-03-30',NULL)`;
con.prepare(prepareStr, function(err, feedback)
{
if (err)
console.log(err);
else
console.log('Statement has been prepared');
});
con.sendLongData('Likes Pig', 0);
con.sendLongData('pig\'s long info', 1);
con.execute(function(err, feedback)
{
if (err)
console.log(err);
else
console.log('Row has been inserted');
});
con.statementClose();
Do not forget to close the prepared statements (construct) after use.
Example of a multi-statement code is:
var selSt = `SELECT * FROM pet WHERE species = 'snake' OR species = 'bird'; SELECT * FROM pet WHERE death IS NOT NULL`;
con.query(selSt, function(err, result, field)
{
if (err)
console.log(err);
else
{
console.log(field);
console.log(result);
}
});
There are two statements in the code. For the callback function (definition), if the first statement returns an error, then err will be set. Otherwise, result and field will each be a three dimensional structure. The outermost structure is an array. It either leads to a single element array of one map or an array of arrays. Each column index of the outermost structure leads to a result, from one of the multi-statements. If the result is OK, then you have an array of single cell array of one map (OK Map). If the result is an error, then you have an array of single cell array of one map, still (Error Map). If the result is a resultset (set of rows), then you have an array of array of maps, where each row is a map. You should try the above code, to appreciate this. The useful result of a stored procedure is similar in explanation.
For the field, each index of the outermost structure corresponds to an array of array. Each array of array gives the field properties of a resultset.
Stored Procedure
Example of a stored procedure code is:
var proceduSt = `CREATE PROCEDURE sampleProc (OUT parA DATE)
BEGIN
SELECT birth
FROM pet
WHERE name = 'Nelly'
INTO parA;
END`;
con.query(proceduSt, function(err, OK)
{
if (err)
console.log(err);
else
console.log(OK);
});
var setStr = `SET @val = NULL`;
con.query(setStr, function(err, OK)
{
if (err)
console.log(err);
else
console.log(OK);
});
var proceduCall = `CALL sampleProc(@val)`;
con.query(proceduCall, function(err, result)
{
if (err)
console.log(err);
else
{
console.log(result);
}
});
var selStr = `SELECT @val`;
con.query(selStr, function(err, result)
{
if (err)
console.log(err);
else
{
console.log(result);
}
});
Transaction
The following is a transaction code:
var transSt = `START TRANSACTION;
SELECT @species := species FROM pet WHERE owner='John';
UPDATE pet SET species=@species WHERE owner='Diane';
COMMIT;`;
con.query(transSt, function(err, OK)
{
if (err)
console.log(err);
else
{
console.log(OK);
}
});
Compression
Supported; see:
Compressed Data Values with EMySQL API
Compressed SQL Statements with EMySQL API
SSL/TLS
Supported; see:
SSL and TLS with EMySQL API
Asynchronous Replication
Supported; see:
Asynchronous Replication with EMySQL
Installation
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 visit the site of the link, Using the EMySQL API, below; after which you start using the API. Your Node.js server directory can be: c:/server.
Download
Click the following link to download the file, after reading the agreement:
EMySQL API
Discussion Group
The link for discussion is:
Coding by Hand
Related Links
Using the EMySQL APIMore Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course
NEXT