当前位置:网站首页>The use of MySQL in nodejs
The use of MySQL in nodejs
2022-07-26 09:47:00 【Color the sky】
1、 install mysql modular
npm install mysql
2、 Use
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);
// take RowDataPacket Object assembly json character string
var string=JSON.stringify(results);
console.log(string);
// take json String to json Array
var json=JSON.parse(string);
console.log(json);
console.log(json.length);
// Traverse json Array
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();
/*
* Add data
*/
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){
/**
Successful show
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 8,
serverStatus: 2,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0 }
*/
console.log(results);
}
});
}
//mysql_update();
/**
* Update data
*/
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){
/**
Modification successful display
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();
/**
* Delete data
*/
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){
/**
Delete successful display
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0 }
*/
console.log(results);
}
});
}
边栏推荐
- MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
- 高斯消元的应用
- Node memory overflow and V8 garbage collection mechanism
- 电机转速模糊pid控制
- 编写一个在bash / shell 和 PowerShell中均可运行的脚本
- JS 一行代码 获取数组最大值与最小值
- Table extraction for opencv table recognition (2)
- 莫队学习笔记(一)
- Fuzzy PID control of motor speed
- Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
猜你喜欢
Login module use case writing
SSG framework Gatsby accesses the database and displays it on the page
【荧光字效果】
服务器、客户端双认证(2)
在Blazor 中自定义权限验证
Fiddler packet capturing tool for mobile packet capturing
Customize permission validation in blazor
MySQL的逻辑架构
Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning
小白搞一波深拷贝 浅拷贝
随机推荐
SSG framework Gatsby accesses the database and displays it on the page
[MySQL] understand the important architecture of MySQL (I)
JS continuous assignment operation
网络流学习笔记
解释一下自动装箱和自动拆箱?
js 表格自动循环滚动,鼠标移入暂停
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
R语言ggpubr包ggsummarystats函数可视化分组箱图(自定义分组颜色)并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr、统计值的色彩和分组图色匹配
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
高斯消元求解矩阵的逆(gauss)
反射机制的原理是什么?
【信息系统项目管理师】初见高项系列精华汇总
Qt随手笔记(二)Edit控件及float,QString转化、
B站这个视频我是跪着看完的
V-for dynamically sets the SRC of img
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
Simple pedestrian recognition code to 88% accuracy Zheng Zhedong preparation
Global variables in DLL
[untitled]
Mo team learning notes (I)