Skip to content

Instantly share code, notes, and snippets.

@luisenriquecorona
Last active May 11, 2019 17:52
Show Gist options
  • Save luisenriquecorona/a79bad298c10d9f2ab47614c6c29fcf2 to your computer and use it in GitHub Desktop.
Save luisenriquecorona/a79bad298c10d9f2ab47614c6c29fcf2 to your computer and use it in GitHub Desktop.
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.
'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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment