Created
November 13, 2013 22:18
-
-
Save iarna/7457528 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Callback form | |
"use strict"; | |
var sqldb = require('sqldb'); | |
sqldb.connect('mysql',{dbname:'test',user:'root'},function(e,dbh){ | |
if (e) throw e; | |
dbh.prepare('SELECT :bar as world',function(e,sth){ | |
if (e) throw e; | |
sth.param('bar','Hello'); | |
sth.execute(function(e,rows){ | |
if (e) throw e; | |
rows.on('data',function (row) { | |
console.log(row); // { world: { '0': 'H', '1': 'e', '2': 'l', '3': 'l', '4': 'o', type: 'VARCHAR' } } | |
// row.world instanceof String == true | |
}); | |
rows.on('error',function (e) { throw e }); | |
rows.on('end',function (e) { dbh.disconnect() }); | |
}) | |
}); | |
}); | |
// with promises | |
"use strict"; | |
var sqldb = require('sqldb'); | |
sqldb.connect('mysql',{dbname:'test',user:'root'}).then(function(dbh) { | |
return dbh.prepare('SELECT :bar as world').then(function (sth) { | |
sth.param('bar',"Hello"); | |
return sth.execute().then(function(rows) { | |
return rows.on('data',function (row) { | |
console.log(row); // { world: { '0': 'H', '1': 'e', '2': 'l', '3': 'l', '4': 'o', type: 'VARCHAR' } } | |
// row.world instanceof String == true | |
}); | |
}) | |
}) | |
.then(function(){ | |
return dbh.disconnect(); | |
}) | |
}) | |
.catch(function (e) { | |
throw e; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment