https://github.com/mysqljs/mysql
How MySql data do, in fact, writing or simply use nodejs operation,
1. Start node in your project npm install mysql --save
2. The introduction of the code in your new project
// introduced database var = MySQL the require ( 'MySQL'); // achieve local link var = mysql.createConnection Connection ({ Host: 'localhost', User: 'YF', password: '123456', Database: 'YF' })
The best will not conflict with root
3. After it is CRUD, attach the code
Inquire
// 查找 function select() { connection.connect(function (err) { if (err) { console.error('error connecting:' + err.stack) } console.log('connected as id ' + connection.threadId); }) connection.query('SELECT * FROM demo', function (error, results, fields) { if (error) throw error; console.log('The solution is:', results); }); connection.end(); }
Add to
//添加 function add() { let post = { id: 1, name: 'Hello MySql', age: 20, time: Date.now(), temp: 'deom' }; let query = connection.query("INSERT INTO demo SET ?", post, function (error, results, fields) { if (error) throw error; }) console.log(query.sql); //INSERT INTO posts 'id'=1, 'title'='Hello MySQL' }
modify
//修改 function updeate() { connection.connect(function (err) { if (err) { console.error('error connecting:' + err.stack); } console.log('connected as id ' + connection.threadId); }); connection.query('UPDATE demo SET name=?where id?', ['update', 1], function (error, results, fields) { if (error) throw error; console.log('changed:' + results.changeRows + 'rows'); }); connection.end(); }
delete
//删除 function deletes() { connection.connect(function (err) { if (err) { console.error('error connecting:' + err.stack); return; } connection.query('DELETE FROM demo SET where id=?', [ 1], function (error, results, fields) { if (error) throw error; console.log('deleted:' + results.affectedRows + 'rows'); }); console.log('connected as id ' + connection.threadId); connection.end(); }); }
Is not that simple ah just add the method name and parameters corresponding to where you need it
How MySql data do, in fact, writing or simply use nodejs operation,
1. Start node in your project npm install mysql --save
2. The introduction of the code in your new project
// introduced database var = MySQL the require ( 'MySQL'); // achieve local link var = mysql.createConnection Connection ({ Host: 'localhost', User: 'YF', password: '123456', Database: 'YF' })
The best will not conflict with root
3. After it is CRUD, attach the code
Inquire
// 查找 function select() { connection.connect(function (err) { if (err) { console.error('error connecting:' + err.stack) } console.log('connected as id ' + connection.threadId); }) connection.query('SELECT * FROM demo', function (error, results, fields) { if (error) throw error; console.log('The solution is:', results); }); connection.end(); }
Add to
//添加 function add() { let post = { id: 1, name: 'Hello MySql', age: 20, time: Date.now(), temp: 'deom' }; let query = connection.query("INSERT INTO demo SET ?", post, function (error, results, fields) { if (error) throw error; }) console.log(query.sql); //INSERT INTO posts 'id'=1, 'title'='Hello MySQL' }
modify
//修改 function updeate() { connection.connect(function (err) { if (err) { console.error('error connecting:' + err.stack); } console.log('connected as id ' + connection.threadId); }); connection.query('UPDATE demo SET name=?where id?', ['update', 1], function (error, results, fields) { if (error) throw error; console.log('changed:' + results.changeRows + 'rows'); }); connection.end(); }
delete
//删除 function deletes() { connection.connect(function (err) { if (err) { console.error('error connecting:' + err.stack); return; } connection.query('DELETE FROM demo SET where id=?', [ 1], function (error, results, fields) { if (error) throw error; console.log('deleted:' + results.affectedRows + 'rows'); }); console.log('connected as id ' + connection.threadId); connection.end(); }); }
Is not that simple ah just add the method name and parameters corresponding to where you need it