测试连接数据库
var mysql = require('mysql'); //调用MySQL模块
//创建一个connection
var connection = mysql.createConnection({
host : 'localhost', //主机
user : 'root', //MySQL认证用户名
password : '123456' //MySQL认证用户密码
});
//创建一个connection
connection.connect(function(err){
if(err){
console.log('[query] - :'+err);
return;
}
console.log('[connection connect] succeed!');
});
//执行SQL语句
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) {
console.log('[query] - :'+err);
return;
}
console.log('The solution is: ', rows[0].solution);
});
//关闭connection
connection.end(function(err){
if(err){
return;
}
console.log('[connection end] succeed!');
});
如果报错
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
mysql -uroot -p
#输入你的密码,假如是:123456
登陆之后,切换成数据库mysql
use mysql;
修改加密方式:
alter user 'root'@'localhost' identified with mysql_native_password by '123456';
注意:123456,应该要换成你自己的root密码
刷新:
flush privileges;
创建数据库
CREATE DATABASE 库名;
创建表
CREATE TABLE dictionary(
_id INT NOT NULL AUTO_INCREMENT,
number VARCHAR(10) NOT NULL,
text VARCHAR(1) NOT NULL,
PRIMARY KEY ( _id )
);
增删改查
var mysql = require('mysql'); //调用MySQL模块
//创建一个connection
var connection = mysql.createConnection({
host: 'localhost', //主机
user: 'root', //MySQL认证用户名
password: '123456', //MySQL认证用户密码
database: 'hanzi'
});
//创建一个connection
connection.connect(function (err) {
if (err) {
console.log('[query] - :' + err);
return;
}
console.log('[connection connect] succeed!');
});
//执行SQL语句
connection.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {
if (err) {
console.log('[query] - :' + err);
return;
}
console.log('The solution is: ', rows[0].solution);
});
// 增
var userAddSql = 'INSERT INTO dictionary(Number,Text) VALUES(?,?)';
var userAddSql_Params = ['0001', '一'];
//增
connection.query(userAddSql, userAddSql_Params, function (err, result) {
if (err) {
console.log('[INSERT ERROR] - ', err.message);
return;
}
console.log('--------------------------INSERT----------------------------');
//console.log('INSERT ID:',result.insertId);
console.log('INSERT ID:', result);
console.log('-----------------------------------------------------------------\n\n');
});
var userModSql = 'UPDATE dictionary SET Text = ? WHERE Number = ?';
var userModSql_Params = ['二', '0001'];
//改
connection.query(userModSql, userModSql_Params, function (err, result) {
if (err) {
console.log('[UPDATE ERROR] - ', err.message);
return;
}
console.log('--------------------------UPDATE----------------------------');
console.log('UPDATE affectedRows', result.affectedRows);
console.log('-----------------------------------------------------------------\n\n');
});
var userGetSql = 'SELECT * FROM dictionary';
//查
connection.query(userGetSql, function (err, result) {
if (err) {
console.log('[SELECT ERROR] - ', err.message);
return;
}
console.log('--------------------------SELECT----------------------------');
console.log(result);
console.log('-----------------------------------------------------------------\n\n');
});
var userDelSql = 'DELETE FROM dictionary';
//删
connection.query(userDelSql, function (err, result) {
if (err) {
console.log('[DELETE ERROR] - ', err.message);
return;
}
console.log('--------------------------DELETE----------------------------');
console.log('DELETE affectedRows', result.affectedRows);
console.log('-----------------------------------------------------------------\n\n');
});
//关闭connection
connection.end(function (err) {
if (err) {
return;
}
console.log('[connection end] succeed!');
});
网友评论