MySQL是常用数据库,作为后端模块,nodejs可以提供了mysql接口

安装

$ npm install mysql

测试代码

var mysql = require('mysql')
// 创建连接
var connection = mysql.createConnection({
    host:'localhost',
    user:'***',
    password:'***',
    database:'cc98'
})

// 连接数据库
connection.connect()
var sql = 'SELECT * from news'
connection.query(sql, function (error, results) {
    if (error)
        throw error
    // 遍历输出条目对象
    for(var i in results){
        console.log(results[i]['part'])
    }
})

// 关闭连接
connection.end()

运行结果

NodeJS 连接接MySQL

小结

node的MySQL接口体验跟R、Python差不多,因此直接看代码就能明白。数据来源见这里

相关文章:

  • 2021-08-13
  • 2022-02-08
  • 2022-12-23
  • 2022-01-17
  • 2021-07-20
  • 2022-02-08
  • 2022-02-08
  • 2022-02-08
猜你喜欢
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-05
相关资源
相似解决方案