You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 插入数据 id自增constsqlAdd=()=>{// posts 表名letsqlAdd='INSERT INTO image SET ?';letsqlAddJson={url: Math.floor(Math.random()*100)+'我是url',title: Math.floor(Math.random()*100)+'我是标题'}connection.query(sqlAdd,sqlAddJson,(err,res)=>{if(err)returnconsole.log('INSERT INFO ERROR:',err.message);console.log('INSERT INFO',res.insertId);});}// sqlAdd()
删
// 删除数据 idconstsqlDelete=id=>{// 通过id查询letsql=`DELETE FROM image WHERE id = ?`;connection.query(sql,[id],(err,res)=>{if(err)console.log('[DELETE ERROR]:',err.message);console.log('DELETE',res.affectedRows);});}// sqlDelete(2)
改
// 更新数据 idconstsqlUpdate=id=>{letsqlAddJson={url: Math.floor(Math.random()*100)+'我是url',title: Math.floor(Math.random()*100)+'我是标题'}// 需要改的数据和查询idletsqlAdd=`UPDATE image SET url = ?, title = ? WHERE id = ?`;connection.query(sqlAdd,[sqlAddJson.url,sqlAddJson.title,id],(err,res)=>{if(err)returnconsole.log('UPDATE ERROR:',err.message);console.log('UPDATE',res.changedRows);});}// sqlUpdate(2)
查
// 查询数据constsqlSelect=()=>{// 查询那个表letsql='SELECT * FROM image';connection.query(sql,(err,result)=>{if(err)console.log('[SELECT ERROR]:',err.message);console.log('result',JSON.stringify(result));//数据库查询结果返回到result中});}// sqlSelect()
end
很简易的增删改查 嘿嘿
The text was updated successfully, but these errors were encountered:
2019-09-02 14:35:40
学习 node mysql, 然后做了一些简单的增删改查, 小小总结总结 Go!!!
资料: https://www.oschina.net/translate/using-node-mysql-javascript-client
init
使用 express 初始化
懒得截图, 运行localhost:3000 就能看到hello world (擅长输出hello world 233)
然后添加了nodemon 自动重启
方便
mysql
npm mysql
文档: https://www.npmjs.com/package/mysql
上面需要根据实际情况修改, 比如说账号密码数据库
数据库我是用 Tool 创建的(不会终端命令 我真垃圾)
然后mysql我是用 brew 下载的 (方便)
这里遇到一个问题
解决方案:
https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server
增
数据新建了一个image, 设置主键ID key 自增
删
改
查
end
很简易的增删改查 嘿嘿
The text was updated successfully, but these errors were encountered: