Sila baca teks penuh untuk pengenalan kepada pangkalan data mysql yang mengendalikan node.js. Pengenalan berikut sangat terperinci, dan kandungan khusus adalah seperti berikut:
Pasang modul mysql
npm install mysql
Penyediaan pangkalan data
Alamat IP mesin di mana pelayan mysql berada ialah 192.168.0.108 Untuk log masuk, gunakan root@123456
Buat pangkalan data ujian dalam mysql
Buat jadual pengguna dalam pangkalan data ujian
Operasi
Sambung ke pangkalan data
var mysql=require('mysql'); var connection = mysql.createConnection({ host : '192.168.0.108', user : 'root', password : '123456', database : 'test1', port:'3306' }); connection.connect();
Masukkan pengguna
var usr={name:'zhangsan',password:'pwdzhangsan',mail:'zhangsan@gmail.com'}; connection.query('insert into users set ?', usr, function(err, result) { if (err) throw err; console.log('inserted zhangsan'); console.log(result); console.log('\n'); });
Kemas kini pengguna dengan syarat
connection.query('update users set password="ddd" where name="zhangsan"', {password:'ppp'}, function(err, result) { if (err) throw err; console.log('updated zhangsan\'s password to ddd'); console.log(result); console.log('\n'); });
Padamkan pengguna dengan syarat
connection.query('delete from users where name="zhangsan"', {password:'ppp'}, function(err, result) { if (err) throw err; console.log('deleted zhangsan'); console.log(result); console.log('\n'); });
Pertanyaan pengguna, semua
connection.query('select * from users', function(err, rows, fields) { if (err) throw err; console.log('selected after deleted'); for(var i= 0,usr;usr=rows[i++];){ console.log('user nae='+usr.name + ', password='+usr.password); } console.log('\n'); });
Tutup sambungan pangkalan data
connection.end();
Penyiapan CRUD asas
Untuk penerangan fungsi lengkap modul mysql, sila lihat rasmi:
https://www.npmjs.com/package/mysql
https://github.com/felixge/node-mysql
Semua kod demo
var mysql=require('mysql'); var connection = mysql.createConnection({ host : '192.168.0.108', user : 'root', password : '123456', database : 'test1', port:'3306' }); connection.connect(); var usr={name:'zhangsan',password:'pwdzhangsan',mail:'zhangsan@gmail.com'}; connection.query('insert into users set ?', usr, function(err, result) { if (err) throw err; console.log('inserted zhangsan'); console.log(result); console.log('\n'); }); connection.query('select * from users', function(err, rows, fields) { if (err) throw err; console.log('selected after inserted'); for(var i= 0,usr;usr=rows[i++];){ console.log('user nae='+usr.name + ', password='+usr.password); } console.log('\n'); }); connection.query('update users set password="ddd" where name="zhangsan"', {password:'ppp'}, function(err, result) { if (err) throw err; console.log('updated zhangsan\'s password to ddd'); console.log(result); console.log('\n'); }); connection.query('select * from users', function(err, rows, fields) { if (err) throw err; console.log('selected after updated'); for(var i= 0,usr;usr=rows[i++];){ console.log('user nae='+usr.name + ', password='+usr.password); } console.log('\n'); }); connection.query('delete from users where name="zhangsan"', {password:'ppp'}, function(err, result) { if (err) throw err; console.log('deleted zhangsan'); console.log(result); console.log('\n'); }); connection.query('select * from users', function(err, rows, fields) { if (err) throw err; console.log('selected after deleted'); for(var i= 0,usr;usr=rows[i++];){ console.log('user nae='+usr.name + ', password='+usr.password); } console.log('\n'); }); connection.end();