'use strict';
var mysql = require('mysql');
var connection = mysql.createConnection({ host: 'localhost',
user: 'root',
password: 'root'
});
connection.query('CREATE DATABASE node', function(err) { if (err) {
console.log('Could not create database "node".');
}
});
connection.query('USE node', function(err) { if (err) {
console.log('Could not switch to database "node".');
}
});
connection.query('CREATE TABLE test ' +
'(id INT(11) AUTO_INCREMENT, ' +
' content VARCHAR(255), ' +
' PRIMARY KEY(id))',
function(err) { if (err) {
console.log('Could not create table "test".');
}
} );
connection.query('INSERT INTO test (content) VALUES ("Hello")');
connection.query('INSERT INTO test (content) VALUES ("World")');
connection.end();
Next, we will query some real data from existing tables. For this, we first need to create a database, add a table to it, and insert some rows. Let’s do this through a Node.js application. Please create a file create.js with the following content
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.