node.js 开发指南 – Node.js 连接 MySQL 并进行数据库操作

 更新时间:2014年07月29日 11:42:38   投稿:hebedich  
通常在NodeJS开发中我们经常涉及到操作数据库,尤其是 MySQL ,作为应用最为广泛的开源数据库则成为我们的首选,本篇就来介绍下如何通过NodeJS来操作 MySQL 数据库。

Node.js是一套用来编写高性能网络服务器的JavaScript工具包
 
通常在NodeJS开发中我们经常涉及到操作数据库,尤其是 MySQL ,作为应用最为广泛的开源数据库则成为我们的首选,本篇就来介绍下如何通过NodeJS来操作 MySQL 数据库。 安装MySQL模块到NodeJS中 我们需要让NodeJS支持MySQL,则需要将MySQL模块添加到系统支持库
 
想要快速了解Node.js ,赞生推荐亲看看 node.js_guide.pdf  — node.js 开发指南 :想要电子版高清的 留言发送
 
如果不想留言 可以带你做飞机! 直接下载
 
Node.js
简单介绍一下node.js的操作吧
安装 node-mysql
C代码  

$ npm install mysql 

 
创建测试表
//数据库名 NodeSample
C代码  

CREATE TABLE `NodeSample`.`MyTable` ( 
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , 
`firstname` VARCHAR( 20 ) NOT NULL , 
`lastname` VARCHAR( 20 ) NOT NULL , 
`message` TEXT NOT NULL 
) ENGINE = MYISAM ; 

 
连接数据库
Js代码  

var sys = require('sys'); 
 
var Client = require('mysql').Client; 
var client = new Client(); 
 
client.user = 'someuser'; 
client.password = 'password'; 
 
client.connect(function(error, results) { 
if(error) { 
console.log('Connection Error: ' + error.message); 
return; 
} 
console.log('Connected to MySQL'); 
}); 

 
打开数据库
Js代码  

ClientConnectionReady = function(client) 
{ 
client.query('USE NodeSample', function(error, results) { 
if(error) { 
console.log('ClientConnectionReady Error: ' + error.message); 
client.end(); 
return; 
} 
}); 
}; 

 
完成数据库操作程序
Js代码 

var sys = require('sys'); 
 
var Client = require('mysql').Client; 
var client = new Client(); 
 
client.user = 'someuser'; 
client.password = 'password'; 
 
console.log('Connecting to MySQL...'); 
 
client.connect(function(error, results) { 
if(error) { 
console.log('Connection Error: ' + error.message); 
return; 
} 
console.log('Connected to MySQL'); 
ClientConnectionReady(client); 
}); 
 
ClientConnectionReady = function(client) 
{ 
client.query('USE NodeSample', function(error, results) { 
if(error) { 
console.log('ClientConnectionReady Error: ' + error.message); 
client.end(); 
return; 
} 
ClientReady(client); 
}); 
}; 
 
ClientReady = function(client) 
{ 
var values = ['Chad', 'Lung', 'Hello World']; 
client.query('INSERT INTO MyTable SET firstname = ?, lastname = ? , message = ?', values, 
function(error, results) { 
if(error) { 
console.log("ClientReady Error: " + error.message); 
client.end(); 
return; 
} 
console.log('Inserted: ' + results.affectedRows + ' row.'); 
console.log('Id inserted: ' + results.insertId); 
} 
); 
GetData(client); 
} 
 
GetData = function(client) 
{ 
client.query( 
'SELECT * FROM MyTable', 
function selectCb(error, results, fields) { 
if (error) { 
console.log('GetData Error: ' + error.message); 
client.end(); 
return; 
} 
// Uncomment these if you want lots of feedback 
//console.log('Results:'); 
//console.log(results); 
//console.log('Field metadata:'); 
//console.log(fields); 
//console.log(sys.inspect(results)); 
 
if(results.length > 0) 
{ 
var firstResult = results[0]; 
console.log('First Name: ' + firstResult['firstname']); 
console.log('Last Name: ' + firstResult['lastname']); 
console.log('Message: ' + firstResult['message']); 
} 
}); 
 
client.end(); 
console.log('Connection closed'); 
}; 

相关文章

  • Node Puppeteer图像识别实现百度指数爬虫的示例

    Node Puppeteer图像识别实现百度指数爬虫的示例

    本篇文章主要介绍了Node Puppeteer图像识别实现百度指数爬虫的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • node指定内存上限简单代码实例

    node指定内存上限简单代码实例

    NodeJS启动的应用,内存使用是有上限的,下面这篇文章主要给大家介绍了关于node指定内存上限的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • 详解node Async/Await 更好的异步编程解决方案

    详解node Async/Await 更好的异步编程解决方案

    这篇文章主要介绍了详解Async/Await 更好的异步编程解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • 基于node.js依赖express解析post请求四种数据格式

    基于node.js依赖express解析post请求四种数据格式

    本篇文章主要介绍了node.js依赖express解析post请求四种数据格式,主要是www-form-urlencoded,form-data,application/json,text/xml,有兴趣的可以了解一下。
    2017-02-02
  • npm发包实践使用gRPC教程

    npm发包实践使用gRPC教程

    这篇文章主要为大家介绍了npm发包实践使用gRPC教程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • node.js命令行教程图文详解

    node.js命令行教程图文详解

    本文先介绍原生的node.js实现命令行交互,了解原生的api,然后通过commander.js和inquirer.js实现一个完整的交互命令行工具。感兴趣的朋友跟随小编一起看看吧
    2019-05-05
  • nodejs npm install全局安装和本地安装的区别

    nodejs npm install全局安装和本地安装的区别

    这篇文章主要介绍了nodejs npm install 全局安装和非全局安装的区别,即带参数-g和不带参数-g安装的区别,需要的朋友可以参考下
    2014-06-06
  • 一文带你了解Node.js进程管理工具PM2

    一文带你了解Node.js进程管理工具PM2

    Node.js进程管理工具PM2是一个开源的工具,用于管理和监控Node.js应用程序的运行,它可以帮助您方便地启动、停止、重启和监视多个Node.js进程,并提供了许多有用的功能,所以本文就和大家一起了解一下PM2,需要的朋友可以参考下
    2023-07-07
  • Node.js pipe实现源码解析

    Node.js pipe实现源码解析

    这篇文章主要介绍了Node.js pipe实现源码解析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • node版本快速切换及管理方法

    node版本快速切换及管理方法

    这篇文章主要为大家介绍了node版本快速切换及管理方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08

最新评论