Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Ape.MySQL

Pablo Tejada edited this page Sep 22, 2013 · 5 revisions

Ape.MySQL( host, username, password, database )

Ape.MySQL is a class constructor. You can use it to connect and use a MySQL database.

Parameters

  • host: Mysql host, can be an ip:port or an unix socket.
  • username: MySQL username.
  • password: MySQL password.
  • database: The database to select.

Example

Database connection
var sql = new Ape.MySQL("ip:port", "user", "password", "database");

sql.onConnect = function() {
    Ape.log('Connected to mysql server');
}
Select request
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>
        });
    }
});
Insert request
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');
});

Ape.MySQL.escape( string )

Escape a string using mysql_escape_string to prepare it for a query. The function returns the escaped string.

Example

sql.query("SELECT text FROM chat WHERE chat_id = "+Ape.MySQL.escape( id ));

MySQL.errorString()

Used to get the last mysql error message. The function returns the MySQL error message

Example

sql.onError = function(errorNo) {
    Ape.log('Connection Error : ' + errorNo + ' : '+ this.errorString());
}

MySQL.getInsertId()

Used to get the last inserted id with auto-increment. The function returns the last inserted id.

MySQL.query( query, callback )

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.

Parameters

  • 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

Example

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');
});

MySQL.onConnect()

This callback is called when mysql connection (and database selection) has been established.

Example

sql.onConnect = function() {
    Ape.log('Connected to mysql server');
}

MySQL.onError()

This callback is called when a mysql error occurs. One paramater is passed to this callback function( errorNum )

Example

sql.onError = function(errorNum) {
    Ape.log('Connection Error : ' + errorNum + ' : '+ this.errorString());
    }

Credit goes to the original authors over at ape-project.org