当前位置:网站首页>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语句的编写,这里就不详细写了
边栏推荐
- Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
- What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
- Kill -9 system call used by PID to kill process
- 二分(整数二分、实数二分)
- Manifest of SAP ui5 framework json
- The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
- 【Swoole系列2.1】先把Swoole跑起来
- 關於這次通信故障,我想多說幾句…
- 递归的方式
- Distinguish between basic disk and dynamic disk RAID disk redundant array
猜你喜欢

Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day

从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...

Rb157-asemi rectifier bridge RB157

C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例

Declval (example of return value of guidance function)

Why does wechat use SQLite to save chat records?

STM32 key state machine 2 - state simplification and long press function addition

1700C - Helping the Nature

Pourquoi Li shufu a - t - il construit son téléphone portable?

递归的方式
随机推荐
面向程序员的精品开源字体
High precision operation
Grafana 9.0 正式发布!堪称最强!
Nodejs 开发者路线图 2022 零基础学习指南
【剑指 Offer】 60. n个骰子的点数
Manifest of SAP ui5 framework json
30 minutes to understand PCA principal component analysis
What is the reason why the video cannot be played normally after the easycvr access device turns on the audio?
The difference between parallelism and concurrency
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
Five data structures of redis
微信为什么使用 SQLite 保存聊天记录?
Interview assault 63: how to remove duplication in MySQL?
Getting started with pytest ----- allow generate report
开源与安全的“冰与火之歌”
Jerry's updated equipment resource document [chapter]
VR panoramic wedding helps couples record romantic and beautiful scenes
Reprint: defect detection technology of industrial components based on deep learning
队列的实现
【Swoole系列2.1】先把Swoole跑起来