nodejs操作mysql

数据库操作
1.需要安装:cnpm install--savedev mysql
2.在index.jszhongyinru mysql = require('mysql');
3.创建数据库连接配置:
connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'',
port:'3306',
database:'数据库名称'
)}
4.连接上数据库
connection.connect()
5.数据库操作:
connection.query('sql语句',arr,function(err,result){
result是返回的数据
})
增:insert into 表名(字段1,字段2...) values(值,值...)
删:delete from 表名 where 条件
改:update 表名 set 字段=值,字段=值 where 条件
查:select 字段 from 表名 where 条件
select * from 表名
select 字段,字段2 from 表名
select * from 表名 where id between 2 and 8
select * from 表名 where id in(4,6,10,13)
select * from 表名 where title like '%关键字%'
select * from 表名 limit '起始位置,条数'
select * from 表名 where id=2 and sex='man'
select * from 表名 where (id=2 and sex='man') or (sex='man' and age=20)

猜你喜欢

转载自blog.csdn.net/amily8023/article/details/80587841