当前位置:网站首页>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);
}
});
}
边栏推荐
- [MySQL] understand the important architecture of MySQL (I)
- IIS website configuration
- 系统安装Serv-U后IIS出错提示:HRESULT:0x80070020
- Sqoop [put it into practice 02] sqoop latest version full database import + data filtering + field type support description and example code (query parameter and field type forced conversion)
- 网站设计需要的基本知识
- “互联网+”时代的现代医学
- The problem of accessing certsrv after configuring ADCs
- [MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
- Smart gourmet C language
- Login module use case writing
猜你喜欢
【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
B站这个视频我是跪着看完的
After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
Redis sentinel mode setup under Windows
Fuzzy PID control of motor speed
Fiddler download and installation
Customize permission validation in blazor
Gauss elimination solves the inverse of matrix (Gauss)
The diagram of user login verification process is well written!
随机推荐
“互联网+”时代的现代医学
编写一个在bash / shell 和 PowerShell中均可运行的脚本
【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)
copyTo
The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
Fuzzy PID control of motor speed
R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)
解决IE7 & IE8 存储cookie问题
js 表格自动循环滚动,鼠标移入暂停
Qt随手笔记(二)Edit控件及float,QString转化、
IIS网站配置
MySQL的逻辑架构
2019 ICPC Asia Yinchuan regional (water problem solution)
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
Registration module use case writing
ie7设置overflow属性失效解决方法
微信小程序AvatarCropper 头像裁剪
Audio and video knowledge
新公链Aptos何以拉满市场期待值?
The whole process of server environment configuration