您的位置:首页 > 博客中心 > 数据库 >

nodejs笔记2-mysql模块

时间:2022-03-13 22:51

1.mysql模块 不是nodejs的官方模块,是属于第三方的模块

 > npm install mysql    //自动安装包括mysql模块依赖的其它的包

其实这安装的就是mysql的一个客户端,

2.  code:    

返回的值: err 如果为null ,操作成功 ,否则失败,

操作成功返回的data数据 是object类型,需要使用 JSON.stringify(data)转换成json格式的字符串。

var mysql = require(‘mysql‘);//引入http模块

var db= mysql.createConnection({host:‘127.0.0.1‘,user:‘root‘,password:‘root‘,database:‘nodejs‘});

db.query("insert into user(username,password,age) values(‘zq‘,‘zqsb‘,25)",function(err,data){
console.log(err);  
console.log(‘\n‘+data)
console.log(‘\n‘);
console.log(JSON.stringify(data));
});

------------------------------------------------------

var mysql = require(‘mysql‘);//引入http模块

var db= mysql.createConnection({host:‘127.0.0.1‘,user:‘root‘,password:‘root‘,database:‘nodejs‘});
db.connect();
db.query("select * from user",function(err,data){
console.log(err);
console.log(‘\n‘);
console.log(JSON.stringify(data));
});
db.end();

nodejs笔记2-mysql模块,布布扣,bubuko.com

热门排行

今日推荐

热门手游