当前位置:网站首页>node の SQLite
node の SQLite
2022-07-06 10:17:00 【华为云】
node操作SQLite
之前在做electron桌面制作番茄钟应用时曾经想过用数据库存储数据,一开始打算mongodb
,但是发现不能实现无服务器,那么只能使用SQLite
了。
介绍:SQLite 是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是在世界上最广泛部署的 SQL 数据库引擎。SQLite 源代码不受版权限制
现在先让我们尝试使用node来操作SQLite
安装sqlite3库
从前面的介绍当中,可以知道,sqlite是免安装的数据库,所以这里使用数据库就只需要添加一个node
的sqlite3
库就可以了。
安装命令:yarn add sqlite3 -D
创建sqlite数据库
当前的node项目地址中并没有sqlite数据库,所以可以通过命令先创建一个数据库,这里使用new sqlite3.Database
来连接数据库,如果当前没有,会自动创建一个数据库,并且不同于之前操作mongodb
,不用再创建一个集合。
const path = require('path');const sqlite3 = require('sqlite3');function SQLiteInit() { let rootPath = path.join(__dirname, '../sqlite3.db'); let db = new sqlite3.Database(rootPath, (err)=>{ if (err) throw err; console.log('数据库连接') })}
效果:
创建表格并插入数据
有了数据库之后,就需要创建一个数据库表格了
可以创建一个sql语句执行方法,此方法可以通过db.run
方法来运行数据sql语句
// 执行sql语句function runSQL(sqlstr) { db.serialize(()=>{ db.run(sqlstr); })}
创建数据表worker
let db = null;function SQLiteInit() { // 连接数据库 let rootPath = path.join(__dirname, '../sqlite3.db'); db = new sqlite3.Database(rootPath) // 创建表格 runSQL(` create table worker ( name text not null, age int not null, hobby text not null ); `) // 关闭连接 db.close();}
现在数据库表格worker已经存在了。
接下来的操作就是在表中插入数据了,这里可以使用db.run
来一次性插入,也可以通过prepare
来分步插入。
let doc = [{ name: '张三', age: 18, hobby: '打李四', }, { name: '李四', age: 18, hobby: '打王五', }, { name: '王五', age: 18, hobby: '打张三', },]let insertInfo = db.prepare('insert into worker (name, age, hobby) values (?, ?, ?)')doc.forEach((item)=>{ insertInfo.run(item.name, item.age, item.hobby);})insertInfo.finalize();
现在数据也已经插入到数据库当中了
vs code
的sqlite
插件
对于数据库,最好使用可视化界面去操作,一般对sqlite
都是推荐用navicate http://www.navicat.com.cn/
不过我这里为了操作快捷,就不用了,在vs code
当中,也存在插件可以对sqlite
数据库进行简单的操作。插件的名称就叫做SQLite
安装完插件之后,如果想要打开之前创建的sqlite3.db
数据库,需要使用Ctrl+Shift+P
打开命令面板,然后输入sqlite
,找到Open Database
选项打开。
这样在左侧资源管理器界面中会出现SQLITE EXPLORER
这里可以查看之前创建的sqlite3.db
数据库中的表格,在右侧选择创建New Query
会创建一个.sql
文件用于执行sql的命令和语句
写入查询表格命令:
-- SQLiteselect * from worker
鼠标右键选择数据库并运行此query
接下来对于数据的查询、删除和更新操作其实都与插入类似,其实主要就是在于sqlite语句的编写,这里就不详细写了
边栏推荐
- 【剑指 Offer】 60. n个骰子的点数
- Getting started with pytest ----- test case rules
- 微信小程序中给event对象传递数据
- MarkDown语法——更好地写博客
- FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
- Grafana 9.0 正式发布!堪称最强!
- The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
- 關於這次通信故障,我想多說幾句…
- Running the service with systemctl in the container reports an error: failed to get D-Bus connection: operation not permitted (solution)
- Interview shock 62: what are the precautions for group by?
猜你喜欢
【Android】Kotlin代码编写规范化文档
Pourquoi Li shufu a - t - il construit son téléphone portable?
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
李書福為何要親自掛帥造手機?
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
30 minutes to understand PCA principal component analysis
队列的实现
Open source and safe "song of ice and fire"
The easycvr authorization expiration page cannot be logged in. How to solve it?
随机推荐
Nodejs 开发者路线图 2022 零基础学习指南
模板于泛型编程之declval
Manifest of SAP ui5 framework json
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
adb常用命令
Running the service with systemctl in the container reports an error: failed to get D-Bus connection: operation not permitted (solution)
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
從交互模型中蒸餾知識!中科大&美團提出VIRT,兼具雙塔模型的效率和交互模型的性能,在文本匹配上實現性能和效率的平衡!...
Interesting - questions about undefined
The difference between parallelism and concurrency
Getting started with pytest ----- allow generate report
Smart street lamp based on stm32+ Huawei cloud IOT design
Declval (example of return value of guidance function)
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
2019 Alibaba cluster dataset Usage Summary
Compilation principle - top-down analysis and recursive descent analysis construction (notes)
面向程序员的精品开源字体
李书福为何要亲自挂帅造手机?
【Android】Kotlin代码编写规范化文档
2022 Summer Project Training (III)