连接数据库:
var mysql = require("mysql"); var connection = mysql.createConnection({ host: "localhost", user: "root", password: "snowball", database: "runoob_test", }); connection.connect((err)=>{ if(err) throw err; console.log('链接成功') });
查询数据表:
var mysql = require("mysql"); var connection = mysql.createConnection({ host: "localhost", user: "root", password: "snowball", database: "runoob_test", }); connection.connect((err)=>{ if(err) throw err; console.log('链接成功') }); // console.log('connection:', connection) // conn.query(sqlStr[,params],callback) // sqlStr:要执行的sql语句,可以使用占位符 // params:参数列表,数组 // callback:回调函数,第一个参数:异常对象;第二个参数:结果对象 // SELECT * FROM要大写 // 查询数据表runoob_tbl let sql = 'SELECT * FROM runoob_tbl' connection.query(sql, function (error, results, fields) { if (error) throw error; console.log('The solution is: ', results); }); // https://www.runoob.com/nodejs/nodejs-mysql.html
插入数据行:
var mysql = require("mysql"); var moment = require('moment'); var connection = mysql.createConnection({ host: "localhost", user: "root", password: "snowball", database: "runoob_test", }); connection.connect((err)=>{ if(err) throw err; console.log('链接成功') }); // console.log('connection:', connection) // conn.query(sqlStr[,params],callback) // sqlStr:要执行的sql语句,可以使用占位符 // params:参数列表,数组 // callback:回调函数,第一个参数:异常对象;第二个参数:结果对象 // SELECT * FROM要大写 // 查询 // let sql = 'SELECT * FROM runoob_tbl' // connection.query(sql, function (error, results, fields) { // if (error) throw error; // console.log('The solution is: ', results); // }); // 插入 // 注意参数、值,个数要正确,格式要正确 console.log(moment().format("YYYY-MM-DD HH:mm:ss")); // var addSql = 'INSERT INTO runoob_tbl(runoob_id,runoob_title,runoob_author,submission_date) VALUES(2,?,?,?)'; var addSql = 'INSERT INTO test_tb2(title,name) VALUES(11,22)'; // var addSqlParams = ['菜鸟工具', 'https://c.runoob.com','23453']; connection.query(addSql,function (err, result) { if(err){ console.log('[INSERT ERROR] - ',err.message); return; } console.log('INSERT-success'); }) // https://www.runoob.com/nodejs/nodejs-mysql.html
改:
var mysql = require("mysql"); var moment = require('moment'); var connection = mysql.createConnection({ host: "localhost", user: "root", password: "snowball", database: "runoob_test", }); connection.connect((err)=>{ if(err) throw err; console.log('链接成功') }); var modSql = 'UPDATE test_tb2 SET title = ?,name = ?'; var modSqlParams = ['nameTest', 'titleTest']; //改 connection.query(modSql,modSqlParams,function (err, result) { if(err){ console.log('[UPDATE ERROR] - ',err.message); return; } console.log('UPDATE affectedRows',result.affectedRows); }); // https://www.runoob.com/nodejs/nodejs-mysql.html
删除:
var mysql = require("mysql"); var moment = require('moment'); var connection = mysql.createConnection({ host: "localhost", user: "root", password: "snowball", database: "runoob_test", }); connection.connect((err)=>{ if(err) throw err; console.log('链接成功') }); var delSql = 'DELETE FROM test_tb2 where title=title'; //改 connection.query(delSql,function (err, result) { if(err){ console.log('[DELETE ERROR] - ',err.message); return; } console.log('DELETE affectedRows',result.affectedRows); }); // https://www.runoob.com/nodejs/nodejs-mysql.html
问题处理:
继续尝试后未解决问题:
检查后发现SELECT * FROM没有大写,大写后问题解决
let sql = 'SELECT * FROM runoob_tbl'
字段不能是sql关键字否则报错,如:desc