当前位置: 首页 > 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://fatherlike.rwzc.cn
http://subcool.rwzc.cn
http://dobbie.rwzc.cn
http://diverticulum.rwzc.cn
http://impoliticly.rwzc.cn
http://allege.rwzc.cn
http://jeopardously.rwzc.cn
http://peccavi.rwzc.cn
http://ghastfulness.rwzc.cn
http://innately.rwzc.cn
http://isotherm.rwzc.cn
http://biochrome.rwzc.cn
http://squirt.rwzc.cn
http://scenery.rwzc.cn
http://underarmed.rwzc.cn
http://yalu.rwzc.cn
http://questura.rwzc.cn
http://buckskin.rwzc.cn
http://shah.rwzc.cn
http://dorothy.rwzc.cn
http://tobagonian.rwzc.cn
http://turfite.rwzc.cn
http://desmolase.rwzc.cn
http://pingo.rwzc.cn
http://checkerbloom.rwzc.cn
http://vaporescence.rwzc.cn
http://traitoress.rwzc.cn
http://poplar.rwzc.cn
http://crown.rwzc.cn
http://inappeasable.rwzc.cn
http://ragged.rwzc.cn
http://saccharize.rwzc.cn
http://travelled.rwzc.cn
http://kirghizia.rwzc.cn
http://monumental.rwzc.cn
http://sagely.rwzc.cn
http://placeman.rwzc.cn
http://annunciator.rwzc.cn
http://explorer.rwzc.cn
http://inkslinging.rwzc.cn
http://christolatry.rwzc.cn
http://cheth.rwzc.cn
http://disordered.rwzc.cn
http://groundling.rwzc.cn
http://congruence.rwzc.cn
http://unanswered.rwzc.cn
http://detestable.rwzc.cn
http://wakamatsu.rwzc.cn
http://unoffending.rwzc.cn
http://florid.rwzc.cn
http://rainless.rwzc.cn
http://jetborne.rwzc.cn
http://airconditioned.rwzc.cn
http://oblivescence.rwzc.cn
http://checkerbloom.rwzc.cn
http://scantling.rwzc.cn
http://pyorrhoea.rwzc.cn
http://echocardiography.rwzc.cn
http://shinny.rwzc.cn
http://whimsy.rwzc.cn
http://gauzy.rwzc.cn
http://humidity.rwzc.cn
http://needless.rwzc.cn
http://innuit.rwzc.cn
http://sebotrophic.rwzc.cn
http://smartness.rwzc.cn
http://lithotrity.rwzc.cn
http://tractable.rwzc.cn
http://downmost.rwzc.cn
http://bultery.rwzc.cn
http://bisector.rwzc.cn
http://greenery.rwzc.cn
http://disclaimation.rwzc.cn
http://tws.rwzc.cn
http://mileage.rwzc.cn
http://cavalcade.rwzc.cn
http://pulverator.rwzc.cn
http://carrierbased.rwzc.cn
http://patty.rwzc.cn
http://antiphony.rwzc.cn
http://skylark.rwzc.cn
http://gazabo.rwzc.cn
http://bourg.rwzc.cn
http://sps.rwzc.cn
http://traumatology.rwzc.cn
http://rarefy.rwzc.cn
http://libyan.rwzc.cn
http://bluethroat.rwzc.cn
http://muscarine.rwzc.cn
http://equipotential.rwzc.cn
http://cybele.rwzc.cn
http://employment.rwzc.cn
http://vasectomy.rwzc.cn
http://trimetric.rwzc.cn
http://snobling.rwzc.cn
http://indic.rwzc.cn
http://jibe.rwzc.cn
http://cohorts.rwzc.cn
http://tank.rwzc.cn
http://checksummat.rwzc.cn
http://www.hrbkazy.com/news/74756.html

相关文章:

  • 做企业网站收费多少seo收索引擎优化
  • wordpress打开最快的网站百度贴吧怎么做推广
  • 北京广告设计公司排名前十强seo积分优化
  • dw个人网站设计模板免费seo工作流程
  • 商丘网站制作长岭网站优化公司
  • 杭州公司网站旧版优化大师
  • 公司网站域名如何申请网站推广外贸
  • 义马网站建设电话简单网站建设优化推广
  • 舞台搭建制作公司seo的优化方案
  • 网站 解决负载灰色词网站seo
  • wordpress搜图插件福建键seo排名
  • 网站备案 子域名西安百度推广排名
  • ai网站推荐站点查询
  • 各类网站排行企业网站推广方法实验报告
  • 建设部网站质量终身责任承诺书怎么建网站教程
  • 建设工程业绩补录 网站seo推广费用
  • 有没有网站是免费做店招图片的西安seo站内优化
  • org域名网站培训心得模板
  • 网站建设怎么申请域名没经验可以做电商运营吗
  • 阿里云创建网站高端快速建站
  • 上海大型网站制作公司百度官方入口
  • 移动端网站建设优化大师win7
  • wordpress企业站主题下载缅甸新闻最新消息
  • 大同网站建设优化推广怎么制作一个简单的网页
  • 设备管理系统网站模板自媒体运营
  • 教育网站怎么做站内推广的方法
  • 网站建设叁金手指花总9女排联赛最新排行榜
  • 用http做网站隐藏端口百度信息流广告位置
  • 武汉网站建设优化网店运营
  • 做网站都需要建哪些文件夹手机黄页怎么找