Files
Node.js-14-Cookbook/Chapter06/mysql/mysql-app/index.js
T
2017-07-31 11:33:31 +05:30

34 lines
654 B
JavaScript

const mysql = require('mysql')
const db = mysql.createConnection({
user: 'root',
//password: 'pw-if-set',
//debug: true
})
db.query('CREATE DATABASE quotes')
db.query('USE quotes')
db.query(`
CREATE TABLE quotes.quotes (
id INT NOT NULL AUTO_INCREMENT,
author VARCHAR ( 128 ) NOT NULL,
quote TEXT NOT NULL, PRIMARY KEY ( id )
)
`)
const ignore = new Set([
'ER_DB_CREATE_EXISTS',
'ER_TABLE_EXISTS_ERROR'
])
db.on('error', (err) => {
if (ignore.has(err.code)) return
throw err
})
db.query(`
INSERT INTO quotes.quotes (author, quote)
VALUES ("Bjarne Stroustrup", "Proof by analogy is fraud.");
`)
db.end()