Skip to content

Instantly share code, notes, and snippets.

@Sannis
Created August 19, 2010 13:26
Show Gist options
  • Save Sannis/537870 to your computer and use it in GitHub Desktop.
Save Sannis/537870 to your computer and use it in GitHub Desktop.
Node-mysql-libmysqlclient v0.0.7 example using
var sys = require("sys");
var mysql_libmysqlclient = require("mysql-libmysqlclient");
var conn = mysql_libmysqlclient.createConnection(host, user, password, database);
if (!conn.connected()) {
sys.puts("Connection error: " + conn.connectErrno() + ", " + conn.connectError());
process.exit(1);
}
var string = conn.escape("Sannis's code");
// Sync queries
var res = conn.query("CREATE TEMPORARY TABLE t1 (alpha INTEGER, beta VARCHAR(255), pi FLOAT);");
sys.puts("'CREATE TABLE' result: " + sys.inspect(res));
res = conn.query("INSERT INTO t1 VALUES (1, 'hello', 3.141);");
sys.puts("LastInsertId: " + sys.inspect(conn.lastInsertId()));
// Async queries
conn.queryAsync("INSERT INTO t1 VALUES (2, 'world', 2.718);", function (res) {
conn.queryAsync("SELECT * FROM t1;", function (res) {
sys.puts("NumRows: " + res.numRows());
var rows = res.fetchAll();
sys.puts("Rows: " + sys.inspect(rows));
conn.queryAsync("SELECT * FROM t1;", function (res) {
var row;
while(row = res.fetchArray()) {
sys.puts("Row: " + sys.inspect(row));
}
conn.queryAsync("DELETE * FROM t1;");
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment