当前位置:网站首页>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);
}
});
}边栏推荐
- matlab simulink实现模糊pid对中央空调时延温度控制系统控制
- 云原生(三十六) | Kubernetes篇之Harbor入门和安装
- js 表格自动循环滚动,鼠标移入暂停
- Wechat applet avatarcropper avatar clipping
- 苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
- SSG框架Gatsby访问数据库,并显示到页面上
- Solve NPM -v sudden failure and no response
- Process32first returns false, error x message 24
- MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
- Calling DLL to start thread
猜你喜欢

苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收

Search module use case writing

Spolicy request case

配置ADCS后访问certsrv的问题

2019 ICPC Asia Yinchuan Regional(水题题解)

电机转速模糊pid控制

Great reward for interview questions

R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)

AR model in MATLAB for short-term traffic flow prediction

spolicy请求案例
随机推荐
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
Mo team learning notes (I)
JS continuous assignment operation
The diagram of user login verification process is well written!
E. Two Small Strings
Gauss elimination solves the inverse of matrix (Gauss)
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
MySQL的逻辑架构
MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
IIS网站配置
POJ 1012 Joseph
Flutter Event 派发
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
Great reward for interview questions
[Online deadlock analysis] by index_ Deadlock event caused by merge
配置ADCS后访问certsrv的问题
js 表格自动循环滚动,鼠标移入暂停
Process32first returns false, error x message 24
Registration module use case writing
R language ggpubr package ggsummarystats function visualizes the grouping box diagram (custom grouping color) and adds the statistical values corresponding to the grouping under the x-axis label (samp