Created
August 19, 2010 13:26
-
-
Save Sannis/537870 to your computer and use it in GitHub Desktop.
Node-mysql-libmysqlclient v0.0.7 example using
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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