-
Notifications
You must be signed in to change notification settings - Fork 6
Ape.MySQL
-
Ape.MySQL » A mysql connection class
-
Ape.MySQL.escape » Escape a string for mysql
-
MySQL.errorString » Get mysql error message
-
MySQL.getInsertId » Get the last id insert with auto-increment
-
MySQL.query » Execute a mysql query
-
MySQL.onConnect » Mysql connection established callback
-
MySQL.onError » Error occurred callback
Ape.MySQL is a class constructor. You can use it to connect and use a MySQL database.
- host: Mysql host, can be an ip:port or an unix socket.
- username: MySQL username.
- password: MySQL password.
- database: The database to select.
var sql = new Ape.MySQL("ip:port", "user", "password", "database");
sql.onConnect = function() {
Ape.log('Connected to mysql server');
}
sql.query("SELECT * FROM table", function(res, errorNo) {
if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString());
else {
Ape.log('Fetching ' + res.length);
res.each(function(data) {
Ape.log(data.content);//data.<column name>
});
}
});
sql.query("INSERT INTO table VALUES('a','b','c')", function(res, errorNo) {
if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString());
else Ape.log('Inserted');
});
Escape a string using mysql_escape_string to prepare it for a query. The function returns the escaped string.
sql.query("SELECT text FROM chat WHERE chat_id = "+Ape.MySQL.escape( id ));
Used to get the last mysql error message. The function returns the MySQL error message
sql.onError = function(errorNo) {
Ape.log('Connection Error : ' + errorNo + ' : '+ this.errorString());
}
Used to get the last inserted id with auto-increment. The function returns the last inserted id.
Used to execute a mysql query, the callback is called when the request ends, if it was a SELECT query, res contains an array of objects.
For now callback function is mandatory, if you don't want your request to have a callback function, use function(){}
as second argument.
- query: The mysql query string.
-
callback: The callback function, two parameters are passed
function( result, errorNum )
.- result: An array of objects if the query was a "SELECT" query
- errorNum: If an error occurred the error code
sql.query("SELECT * FROM table", function(res, errorNo) {
if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString());
else {
Ape.log('Fetching ' + res.length);
for(var i = 0; i < res.length; i++) {
Ape.log(res[i].title);//res[i].<column name>
});
}
});
sql.query("INSERT INTO table VALUES('a','b','c')", function(res, errorNo) {
if (errorNo) Ape.log('Request error : ' + errorNo + ' : '+ this.errorString());
else Ape.log('Inserted');
});
This callback is called when mysql connection (and database selection) has been established.
sql.onConnect = function() {
Ape.log('Connected to mysql server');
}
This callback is called when a mysql error occurs. One paramater is passed to this callback function( errorNum )
sql.onError = function(errorNum) {
Ape.log('Connection Error : ' + errorNum + ' : '+ this.errorString());
}
Credit goes to the original authors over at ape-project.org