当前位置:网站首页>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);
}
});
}
边栏推荐
- 原根与NTT 五千字详解
- jvm命令归纳
- 大二上第三周学习笔记
- Exception handling mechanism II
- 【Mysql】一条SQL语句是怎么执行的(二)
- VectorTileLayer更换style
- Sliding window, double pointer, monotone queue, monotone stack
- 自定义密码输入框,无圆角
- Ext4 file system opens dir_ After nlink feature, link_ Use link after count exceeds 65000_ Count=1 means the quantity is unknown
- Grain College of all learning source code
猜你喜欢
(2006, MySQL server has gone away) problem handling
arcgis的基本使用4
arc-gis的基本使用2
Nuxt - Project packaging deployment and online to server process (SSR server rendering)
服务器内存故障预测居然可以这样做!
Innovus is stuck, prompting x error:
【Mysql】redo log,undo log 和binlog详解(四)
Windows backs up the database locally by command
Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
Unity topdown character movement control
随机推荐
大二上第三周学习笔记
The Child and Binary Tree-多项式开根求逆
Nuxt - Project packaging deployment and online to server process (SSR server rendering)
[MySQL] detailed explanation of redo log, undo log and binlog (4)
MySQL 强化知识点
jvm命令归纳
When you click input, the border is not displayed!
[use of final keyword]
Use of off heap memory
Server memory failure prediction can actually do this!
小程序纪录
838. 堆排序
Qtcreator reports an error: you need to set an executable in the custom run configuration
arc-gis基础操作3
TableviewCell高度自适应
大二上第五周学习笔记
LeetCode三数之和问题
Canal 的学习笔记
2022 tea artist (intermediate) special operation certificate examination question bank simulated examination platform operation
Object type collections are de duplicated according to the value of an attribute