mysql基础
- 导入sql数据
显示数据库: show batabases;
选择数据库: use 数据库名称;
导入数据: source sql文件路径;
查看数据是否导入成功:show tables;![请添加图片描述](https://www.icode9.com/i/ll/?i=1e9be0c5f56f46ac9805bdbae8ccbc4c.png?,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAcXFfNDM3MzExNzY=,size_20,color_FFFFFF,t_70,g_se,x_16)
2.node连接mysql数据库CURD操作
(1)查
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user:'root',
password:'********',
port:'8080',
database: 'test'
});
connection.connect();
connection.query('SELECT * FROM websites',(err,res)=>{
if(err) {
console.log('[SELECT ERROR] - ',err.message);
return
}
console.log(JSON.stringify(res)+'\n');
})
(2)增
//第一种方法
connection.query('insert into websites(id,name,url,alexa,country) value(6,"QQ","http://qq.com/","1","china")',(err,res)=>{
if(err) {
console.log('[SELECT ERROR] - ',err.message);
return
}
});
// 第二种方法
const sql = 'insert into websites(id,name,url,alexa,country) value(6,?,?,?,?)';
const sqlparams = ["QQ","http://qq.com/","1","china"];
connection.query(sql,sqlparams,(err,res)=>{
if(err) {
console.log('[SELECT ERROR] - ',err.message);
return
}
});
3.改
//改
connection.query('update websites set name= ?,url = ? where id = ?',["wx","http:wx.com/",6],(err,res)=>{
if(err) {
console.log('[SELECT ERROR] - ',err.message);
return
}
console.log(JSON.stringify(res)+'\n');
})
4.删
// 查
connection.query('SELECT * FROM websites',(err,res)=>{
if(err) {
console.log('[SELECT ERROR] - ',err.message);
return
}
console.log(JSON.stringify(res)+'\n');
})