当前位置:网站首页>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语句的编写,这里就不详细写了
边栏推荐
- How to solve the error "press any to exit" when deploying multiple easycvr on one server?
- Four processes of program operation
- Getting started with pytest ----- test case pre post, firmware
- Jerry's updated equipment resource document [chapter]
- VR panoramic wedding helps couples record romantic and beautiful scenes
- Why does wechat use SQLite to save chat records?
- Codeforces Round #803 (Div. 2)
- Olivetin can safely run shell commands on Web pages (Part 1)
- Recommend easy-to-use backstage management scaffolding, everyone open source
- Common - magic number 7
猜你喜欢

Transport layer congestion control - slow start and congestion avoidance, fast retransmission, fast recovery

Getting started with pytest ----- test case pre post, firmware

Introduction to the usage of model view delegate principal-agent mechanism in QT

QT中Model-View-Delegate委托代理机制用法介绍

Grafana 9.0 正式发布!堪称最强!

2019阿里集群数据集使用总结

Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability

win10系统下插入U盘有声音提示却不显示盘符

Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology

C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例
随机推荐
趣-关于undefined的问题
C语言自动预订飞机票问题
win10系统下插入U盘有声音提示却不显示盘符
1700C - Helping the Nature
Maixll-Dock 摄像头使用
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
Cool Lehman has a variety of AI digital human images to create a vr virtual exhibition hall with a sense of technology
2022暑期项目实训(三)
2022 Summer Project Training (I)
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
【Android】Kotlin代码编写规范化文档
d绑定函数
Distinguish between basic disk and dynamic disk RAID disk redundant array
The easycvr authorization expiration page cannot be logged in. How to solve it?
Insert dial file of Jerry's watch [chapter]
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
Nodejs 开发者路线图 2022 零基础学习指南
Is it meaningful for 8-bit MCU to run RTOS?
C language exchanges two numbers through pointers
Appium automated test scroll and drag_ and_ Drop slides according to element position