当前位置:网站首页>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);
}
});
}边栏推荐
- Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points
- Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
- Redis principle and usage - installation and distributed configuration
- js中树与数组的相互转化(树的子节点若为空隐藏children字段)
- Grain College of all learning source code
- Does volatile rely on the MESI protocol to solve the visibility problem? (next)
- 大二上第二周学习笔记
- 756. 蛇形矩阵
- Tornado multi process service
- Basic use of ArcGIS 1
猜你喜欢

Canal 的学习笔记

What is the difference between NFT and digital collections?

arcgis的基本使用1

Sending and receiving of C serialport

Server memory failure prediction can actually do this!
![[shutter -- layout] detailed explanation of the use of align, center and padding](/img/01/c588f75313580063cf32cc01677600.jpg)
[shutter -- layout] detailed explanation of the use of align, center and padding

jvm命令归纳

Mysql事务

CF1481C Fence Painting

arcgis的基本使用4
随机推荐
Android implements the caching mechanism and caches multiple data types
Processing of inconsistent week values obtained by PHP and MySQL
原根与NTT 五千字详解
什么是异步操作
神经网络与深度学习-6- 支持向量机1 -PyTorch
"Could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32"
Custom password input box, no rounded corners
Nuxt - Project packaging deployment and online to server process (SSR server rendering)
Order based evaluation index (especially for recommendation system and multi label learning)
opencv图像处理
[MySQL] how to execute an SQL statement (2)
What is asynchronous operation
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
Vertical search
I'm faded
Under a directory of ext3 file system, subfolders cannot be created, but files can be created
[online problem] timeout waiting for connection from pool problem troubleshooting
设置视图动态图片
省政府召开全省高温天气安全防范工作电视电话会议
Cat installation and use