nodejs_succinctly

Page 107

var sqlite3 = require('sqlite3').verbose(); var db = new sqlite3.Database('mydatabase'); db.serialize(function(){ // Create the table. db.run('CREATE TABLE customer(id NUMERIC,name TEXT)'); // Insert the data. var query = db.prepare('INSERT INTO customer VALUES(?,?)'); for(var i=0;i<5;i++){ query.run(i+1,'customer ' + (i+1)); } query.finalize(); // Select the data. db.each('SELECT * FROM customer', function(err,row){ if(!err){ console.log(row.id + '----' + row.name); }else{ console.log(err); } }); }); db.close();

MongoDB To access MongoDB, we can use the node-mongodb-native module as the database driver. You can get further information about this module at https://github.com/christkv/node-mongodbnative. First, install MongoDB. If you have already installed it, skip these steps. For the Ubuntu platform, update the repository: sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10 sudo apt-get update Install MongoDB and its driver for Node.js: sudo apt-get install mongodb-10gen sudo npm install mongodb

107


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.