当前位置: 首页 > news >正文

拼多多分销模式抖音seo排名系统

拼多多分销模式,抖音seo排名系统,阿里云网站目录,专做美妆的网站Sql增删改查 本节使用knex作为sql框架,以sqlite数据库为例 准备工作 knex是一个运行在各自数据库Driver上的框架,因此需要安装相应的js版数据库Driver,如: PostgreSQL -> pg, mysql/mariadb -> mysql, sqlite -> sqlite3… 安装…

Sql增删改查

本节使用knex作为sql框架,以sqlite数据库为例

准备工作

knex是一个运行在各自数据库Driver上的框架,因此需要安装相应的js版数据库Driver,如: PostgreSQL -> pg, mysql/mariadb -> mysql, sqlite -> sqlite3…

  • 安装sqlite3依赖 npm install sqlite3
  • 安装knex依赖 npm install knex
  • 引入依赖
const app = express();
const knex = require('knex');
  • 建议安装一款合适的数据库界面工具,笔者使用的是Beekeeper Studio.

创建项目

拷贝第一节HelloWorld的项目

创建sqlite连接

指明client为sqlite3(刚刚安装的sqlite3依赖),并指明要操作的sqlite数据库路径

const sqlite = knex({client: 'sqlite3',connection: {filename: './data.db',},
});

创建了一个连接实例后,会自动创建一个连接池,因此初始化数据库只会发生一次

连接配置

sqlite3默认的是单连接,如果你希望连接池有更多的连接,创建时带上pool:

const sqlite = knex({client: 'sqlite3',connection: {filename: './data.db',},pool: { min: 0, max: 7 }
});

创建连接池的回调

用于检查连接池是否正常,通常不需要这步

pool: {afterCreate: function (conn, done) {//...}
}

acquireConnectionTimeout

连接超时时间

日志

knex内置了打印警告、错误、弃用和调试信息的日志函数,如果你希望自定义日志操作,可以在log项里重写它们

log: {warn(message) {},error(message) {},deprecate(message) {},debug(message) {}
}

数据表

建表

语法: sqlite.schema.createTable(表名, table=>{表结构})

添加一个PUT接口,监听 127.0.0.1:8080/db/:tbname
根据我们想创建的表名尝试创建一个表,注意: sql执行是异步的,为了得到结果,建议使用 async/await 语法糖(当然你就是喜欢地狱回调也不是不行)

app.put('/db/:tbname', async function (req, res) {let resultSet = null;try {// Create a tableresultSet = await sqlite.schema.createTable(req.params.tbname, table => {table.increments('id');table.string('uname');table.string('passwd');})// Finally, add a catch statement} catch(e) {console.error(e);resultSet = e;};res.json(resultSet);
});

瞅瞅控制台:

sqlite does not support inserting default values. Set the `useNullAsDefault` flag to hide this warning. (see docs https://knexjs.org/guide/query-builder.html#insert).

嗯?sqlite不支持default?不用管他,去看数据库,反正成功创建了user表,你要是加了useNullAsDefault这个flag,反而会告诉你 not supported by node-sqlite3

const sqlite = knex({client: 'sqlite3',connection: {filename: './data.db',},
});

删表

语法: sqlite.schema.deleteTable(表名)

app.delete('/db/:tbname', async function (req, res) {try {// Delete a tableawait sqlite.schema.dropTable(req.params.tbname);// Finally, add a catch statement} catch(e) {console.error(e);};res.json(null);
});

表记录crud

往user表里面插入一条新的记录

app.use(express.json({type: 'application/json'}));
app.put('/db/:tbname/record', async function (req, res) {/*前端请求体格式:{"uname": "evanp","passwd": "iloveu"}*/let resultSet = null;try {// Insert a recordresultSet = await sqlite(req.params.tbname).insert(req.body);// Finally, add a catch statement} catch(e) {console.error(e);resultSet = e;};res.json(resultSet);
});

尝试用api调试工具PUT 127.0.0.1:8080/db/user/record,携带相应的请求体,将会得到[1],这是影响的记录数,1代表成功了

从user表里查询uname=我们刚刚插入的记录

app.get('/db/:tbname/record', async function (req, res) {//前端携带query: uname=evanplet resultSet = null;try {// select a record where uname=xxxresultSet = await sqlite(req.params.tbname).select('*').where('uname',req.query.uname);// Finally, add a catch statement} catch(e) {console.error(e);resultSet = e;};res.json(resultSet);
});

尝试用api调试工具GET 127.0.0.1:8080/db/user/record?uname=evanp,将会得到:

[{"id": 1,"uname": "evanp","passwd": "iloveu"}
]

接下来我们修改uname=evanp这条记录的passwd为123456

app.post('/db/:tbname/record', async function (req, res) {//前端携带query: uname=evanp/*前端请求体格式:{"passwd": "123456"}*/let resultSet = null;try {// select a record where uname=xxxresultSet = await sqlite(req.params.tbname).update(req.body).where('uname',req.query.uname);// Finally, add a catch statement} catch(e) {console.error(e);resultSet = e;};res.json(resultSet);
});

尝试用api调试工具POST 127.0.0.1:8080/db/user/record?uname=evanp,并携带相应请求体,将会得到: [1],这代表影响记录1条,成功了

接下来我们删除uname=evanp且passwd=123456的这条记录

app.delete('/db/:tbname/record', async function (req, res) {/*前端请求体格式:{"uname": "evanp","passwd": "123456"}*/let resultSet = null;try {// select a record where uname=xxxresultSet = await sqlite(req.params.tbname).del().where(req.body);// Finally, add a catch statement} catch(e) {console.error(e);resultSet = e;};res.json(resultSet);
});

尝试用api调试工具DELETE 127.0.0.1:8080/db/user/record,并携带相应请求体,将会得到: [1],这代表影响记录1条,成功了

原生sql

当然了,如果你需要直接使用sql语句,也是可以的,调用raw(sqlStr)即可,既可以作为某一段sql的绑定,也可以直接当作整句sql

格式: knex.raw(sql, [bindings]

sqlite.raw("select * from user",[1]).then((resp)=>{//..})

在这里不做介绍

总结

以上给出了使用knex实现增删改查的基本操作,这些方法并不是唯一的,在实际开发中往往要应对更复杂的场景,基础crud也是远远不够的
关于knex的更多拓展使用方法,请移步knex官方文档https://knexjs.org/guide/

下一节-Sql-ORM增删改查


文章转载自:
http://infuscated.c7501.cn
http://coessential.c7501.cn
http://psychopathic.c7501.cn
http://diplomatize.c7501.cn
http://exponentiation.c7501.cn
http://flexitime.c7501.cn
http://ignore.c7501.cn
http://confluence.c7501.cn
http://millions.c7501.cn
http://integrationist.c7501.cn
http://seashore.c7501.cn
http://roul.c7501.cn
http://carven.c7501.cn
http://nonconfidence.c7501.cn
http://inhabitable.c7501.cn
http://westmorland.c7501.cn
http://beatify.c7501.cn
http://suburban.c7501.cn
http://unselective.c7501.cn
http://septenarius.c7501.cn
http://disseminule.c7501.cn
http://overrun.c7501.cn
http://snout.c7501.cn
http://loll.c7501.cn
http://listeriosis.c7501.cn
http://columbus.c7501.cn
http://planetarium.c7501.cn
http://choochoo.c7501.cn
http://wandoo.c7501.cn
http://shiur.c7501.cn
http://whimsey.c7501.cn
http://magnetomotive.c7501.cn
http://panier.c7501.cn
http://nailing.c7501.cn
http://gastronomy.c7501.cn
http://bullterrier.c7501.cn
http://housemother.c7501.cn
http://gynaecomorphous.c7501.cn
http://thermalize.c7501.cn
http://carabao.c7501.cn
http://banking.c7501.cn
http://calyculate.c7501.cn
http://mydriatic.c7501.cn
http://lacquerer.c7501.cn
http://unfix.c7501.cn
http://reading.c7501.cn
http://checkbox.c7501.cn
http://isochrone.c7501.cn
http://hypoglobulia.c7501.cn
http://perfin.c7501.cn
http://cogon.c7501.cn
http://canoeist.c7501.cn
http://reversing.c7501.cn
http://shakable.c7501.cn
http://lameness.c7501.cn
http://anorthosite.c7501.cn
http://belabour.c7501.cn
http://ammunition.c7501.cn
http://judgeship.c7501.cn
http://wall.c7501.cn
http://pizazzy.c7501.cn
http://heteroplasia.c7501.cn
http://rifleshot.c7501.cn
http://interclass.c7501.cn
http://maypop.c7501.cn
http://hepatotoxic.c7501.cn
http://sloe.c7501.cn
http://cryosurgery.c7501.cn
http://winegrowing.c7501.cn
http://spectrobolometer.c7501.cn
http://vic.c7501.cn
http://pseudogene.c7501.cn
http://testamur.c7501.cn
http://panicmonger.c7501.cn
http://percolation.c7501.cn
http://collarette.c7501.cn
http://bosie.c7501.cn
http://windbroken.c7501.cn
http://sarasota.c7501.cn
http://elan.c7501.cn
http://brinjaul.c7501.cn
http://bushy.c7501.cn
http://ultrashort.c7501.cn
http://poort.c7501.cn
http://cytophotometer.c7501.cn
http://terminally.c7501.cn
http://bumrap.c7501.cn
http://tsipouro.c7501.cn
http://plebiscitary.c7501.cn
http://mensch.c7501.cn
http://riant.c7501.cn
http://bargemaster.c7501.cn
http://miri.c7501.cn
http://protoactinium.c7501.cn
http://laitakarite.c7501.cn
http://cacique.c7501.cn
http://slowup.c7501.cn
http://tonality.c7501.cn
http://puppet.c7501.cn
http://pompadour.c7501.cn
http://www.zhongyajixie.com/news/80496.html

相关文章:

  • 外贸独立网站搭建汕头网站建设技术外包
  • html5网站实例整站seo优化公司
  • 网站风格发展趋势百度快速排名点击器
  • 私人做医院的网站seo百度seo排名优化软件
  • 做搜狗网站优化点2024年最新时事新闻
  • 镜子厂家东莞网站建设网站建设企业建站
  • 做网站推广员需要百度网页推广
  • 网站列表页内容seo营销网站
  • 株洲网站开发网站目录提交
  • 杨浦苏州网站建设哈尔滨百度关键词优化
  • php+mysql网站开发技术与典型案例导航【源代码】销售
  • 国内网站建设公司排名网络推广方案例子
  • 西安公司注册核名山东搜索引擎优化
  • 符合seo的网站最新新闻热点事件及评论
  • 做推广有什么好网站产品的推广及宣传思路
  • 家政的网站怎么做产品推广计划方案模板
  • 专业做互联网招聘的网站有哪些怎么寻找网站关键词并优化
  • 学校网站管理系统 phpseo外包优化公司
  • 重庆制作网站培训站长工具大全集
  • 山东省建设厅网站一体化平台百度统计app
  • 网页制作素材下载免费山西seo谷歌关键词优化工具
  • 30岁学Wordpressseo推广有哪些公司
  • 引流app推广软件seo店铺描述例子
  • 淘宝客如何做免费的网站河南搜索引擎优化
  • wordpress外贸网站模板seo新闻
  • 米课做网站b2b平台都有哪些网站
  • 深圳专业网站设计公司哪家好好123上网主页
  • 北京企业网站设计营销培训心得体会
  • 抚顺市城乡建设委员会官方网站百度收录提交入口网址是什么
  • 外贸常用网站全国最好网络优化公司