当前位置:网站首页>nodejs中mysql的使用
nodejs中mysql的使用
2022-07-26 09:23:00 【为天空着色】
1、安装mysql模块
npm install mysql
2、使用
var mysql=require('mysql');
var prefix='socket';
//var database='nodejs';
var table_user=prefix+'_user';
var client=mysql.createConnection({
host:'127.0.0.1',
port:'3306',
user:'nodejs',
password:'nodejs',
database:'nodejs',
});
client.connect();
//client.query('use '+database);
client.query('select * from '+table_user,function(err, results, fields){
if(err){
throw err;
}
console.log(fields);
if (results) {
//console.log(results);
//将RowDataPacket对象装化成json字符串
var string=JSON.stringify(results);
console.log(string);
//将json字符串转化成json数组
var json=JSON.parse(string);
console.log(json);
console.log(json.length);
//遍历json数组
for (var i =0; i <json.length; i++) {
console.log('%d\t%s\t%s',json[i].id,json[i].username,json[i].password);
};
}
});
//mysql_add();
/*
* 添加数据
*/
function mysql_add(){
var insersql='insert into '+table_user+'(`username`,`password`,`create_time`,`online`) values(?,?,UNIX_TIMESTAMP(now()),?)';
var param=['nodejs2','nodejs_password','2'];
client.query(insersql,param,function(err,results){
if(err){
throw err;
}
if(results){
/**
成功显示
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 8,
serverStatus: 2,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0 }
*/
console.log(results);
}
});
}
//mysql_update();
/**
* 更新数据
*/
function mysql_update(){
var updatesql='update '+table_user+' set online=?,create_time=UNIX_TIMESTAMP(now()) where id=?';
var param=['2','1'];
client.query(updatesql,param,function(err,results){
if(err){
throw err;
}
if(results){
/**
修改成功显示
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '(Rows matched: 1 Changed: 1 Warnings: 0',
protocol41: true,
changedRows: 1 }
*/
console.log(results);
console.log(JSON.parse(JSON.stringify(results)));
}
});
}
//mysql_delete();
/**
* 删除数据
*/
function mysql_delete(){
var deletesql='delete from '+table_user+' where id=?';
var param=['2'];
client.query(deletesql,param,function(err,results){
if(err){
throw err;
}
if(results){
/**
删除成功显示
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0 }
*/
console.log(results);
}
});
}
边栏推荐
- Local cache
- [MySQL] detailed explanation of redo log, undo log and binlog (4)
- 自定义密码输入框,无圆角
- Unity topdown character movement control
- "No input file specified" problem handling
- Object type collections are de duplicated according to the value of an attribute
- Vertical search
- Basic use of ArcGIS 4
- [arkit, realitykit] turn pictures into 3D models
- 756. 蛇形矩阵
猜你喜欢
Unity topdown character movement control
Windows backs up the database locally by command
【Mysql】一条SQL语句是怎么执行的(二)
Object 的Wait Notify NotifyAll 源码解析
Polynomial open root
2022 mobile crane driver test question simulation test question bank simulation test platform operation
CF1481C Fence Painting
Innovus is stuck, prompting x error:
The child and binary tree- open root inversion of polynomials
语音聊天app源码——钠斯直播系统源码
随机推荐
Polynomial open root
Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points
The problem of the sum of leetcode three numbers
js在控制台输出菱形
2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
点击input时,不显示边框!
756. Serpentine matrix
a-table中的rowSelection清空问题
Selection and practice of distributed tracking system
(2006, MySQL server has gone away) problem handling
省政府召开全省高温天气安全防范工作电视电话会议
“No input file specified “问题的处理
微信小程序学习笔记2
什么是异步操作
服务器内存故障预测居然可以这样做!
自定义密码输入框,无圆角
MySQL 强化知识点
[arkit, realitykit] turn pictures into 3D models
【Mysql】认识Mysql重要架构(一)
2022 Shanghai safety officer C certificate examination questions and mock examination