关于node.js操作MysqL数据库的相关介绍请阅读全文吧。下文介绍的非常详细,具体内容如下所示:
安装MysqL模块
MysqL
数据库准备
MysqL server所在的机器IP地址是192.168.0.108,登录账户就用root@123456
在test数据库中创建一张users表
操作
连接数据库
MysqL=require('MysqL');
var connection = MysqL.createConnection({
host : '192.168.0.108',user : 'root',password : '123456',database : 'test1',port:'3306'
});
connection.connect();
插入一个user
更新user,带条件
删除一个user,带条件
查询user,所有
connection.end();
基本的CRUD完成
全部demo代码
MysqL=require('MysqL');
var connection = MysqL.createConnection({
host : '192.168.0.108',port:'3306'
});
connection.connect();
var usr={name:'zhangsan',result) {
if (err) throw err;
console.log('inserted zhangsan');
console.log(result);
console.log('\n');
});
connection.query('select * from users',fields) {
if (err) throw err;
console.log('selected after inserted');
for(var i= 0,password='+usr.password);
}
console.log('\n');
});
connection.query('update users set password="ddd" where name="zhangsan"',result) {
if (err) throw err;
console.log('updated zhangsan\'s password to ddd');
console.log(result);
console.log('\n');
});
connection.query('select * from users',fields) {
if (err) throw err;
console.log('selected after updated');
for(var i= 0,password='+usr.password);
}
console.log('\n');
});
connection.query('delete from users where name="zhangsan"',result) {
if (err) throw err;
console.log('deleted zhangsan');
console.log(result);
console.log('\n');
});
connection.query('select * from users',password='+usr.password);
}
console.log('\n');
});
connection.end();