首页 > 代码库 > node + mysql
node + mysql
1.package.json
{ "name": "nodezbj", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "superagent": "3.5.1", "mysql": "2.13.0", "cheerio": "0.22.0" } }
2.在 lib 里面新建 sql.js
var mysql = require(‘mysql‘); function SQL(callback) { var connection = mysql.createConnection({ host: ‘‘, user: ‘root‘, password: ‘‘, database: ‘‘ }); connection.connect(); callback.call(null, connection); connection.end(); } module.exports = { query: function(str, callback) { SQL(function(connection) { connection.query(str, callback); }); } };
3.node 引用 mysql
var request = require(‘superagent‘); var cheerio = require(‘cheerio‘) var sql = require(‘../lib/sql.js‘) request .get(‘http://‘) .end(function(err, res) { if (err) { console.log(err); return; } var $ = cheerio.load(res.text); var cateDiv = $(‘.list-category-nav .unstyled.ui-dropdown-menu a‘); cateDiv.each(function(index, item) { var text = $(item).text(); var href = http://www.mamicode.com/$(item).attr(‘href‘); console.log(text, href); sql.query(‘INSERT INTO cate1 (name, url) VALUES ("‘ + text + ‘", "‘ + href + ‘")‘); }); });
node + mysql
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。