当前位置:网站首页>node の SQLite
node の SQLite
2022-07-06 18:34:00 【Hua Weiyun】
node operation SQLite
Before doing electron When making tomato clock application on the desktop, I thought about using database to store data , At first mongodb, But it is found that no server can be realized , Then only use SQLite 了 .
Introduce :SQLite It's a software library , It's self-sufficient 、 serverless 、 Zero configuration 、 transactional SQL Database engine .SQLite It's the most widely deployed in the world SQL Database engine .SQLite Source code is not restricted by copyright
Now let's try to use node To operate SQLite
install sqlite3 library
From the previous Introduction , You can know ,sqlite It is an installation free database , Therefore, to use the database here, you only need to add one node Of sqlite3 Library will do .
Installation command :yarn add sqlite3 -D

establish sqlite database
Current node There is no... In the project address sqlite database , So you can create a database by using the command , Use here new sqlite3.Database To connect to the database , If there is no , Will automatically create a database , And different from the previous operation mongodb, There is no need to create a collection .
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(' Database connection ') })}effect :

Create a table and insert data
With the database , You need to create a database table
You can create a sql Statement execution method , This method can be achieved by db.run Method to run data sql sentence
// perform sql sentence function runSQL(sqlstr) { db.serialize(()=>{ db.run(sqlstr); })}Create data table worker
let db = null;function SQLiteInit() { // Connect to database let rootPath = path.join(__dirname, '../sqlite3.db'); db = new sqlite3.Database(rootPath) // Create a table runSQL(` create table worker ( name text not null, age int not null, hobby text not null ); `) // Close the connection db.close();}Now the database tables worker It already exists .
The next operation is to insert data into the table , Here you can use db.run To insert... At one time , It can also be done through prepare To insert... Step by step .
let doc = [{ name: ' Zhang San ', age: 18, hobby: ' Hit Li Si ', }, { name: ' Li Si ', age: 18, hobby: ' Beat Wang Wu ', }, { name: ' Wang Wu ', age: 18, hobby: ' Open three ', },]let insertInfo = db.prepare('insert into worker (name, age, hobby) values (?, ?, ?)')doc.forEach((item)=>{ insertInfo.run(item.name, item.age, item.hobby);})insertInfo.finalize();Now the data has been inserted into the database
vs code Of sqlite plug-in unit
For the database , It is best to use a visual interface to operate , General pair sqlite Are recommended navicate http://www.navicat.com.cn/
But I'm here for quick operation , No need , stay vs code among , There are also plug-ins that can be used to sqlite Simple operation of database . The name of the plug-in is SQLite

After installing the plug-in , If you want to open the sqlite3.db database , Need to use Ctrl+Shift+P Open the command panel , Then input sqlite, find Open Database Options on .

This will show up in the left explorer interface SQLITE EXPLORER
Here you can view the previously created sqlite3.db Tables in the database , Select Create... On the right New Query

Will create a .sql File for execution sql Commands and statements
Write query table command :
-- SQLiteselect * from workerRight click to select the database and run this query


Next, query the data 、 Delete and update operations are similar to insert , In fact, the main thing is sqlite Statement writing , I won't go into details here
边栏推荐
猜你喜欢

CSRF vulnerability analysis

【Swoole系列2.1】先把Swoole跑起来

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

虚拟机VirtualBox和Vagrant安装

1700C - Helping the Nature

Top command details

Splay

Windows connects redis installed on Linux

從交互模型中蒸餾知識!中科大&美團提出VIRT,兼具雙塔模型的效率和交互模型的性能,在文本匹配上實現性能和效率的平衡!...

Distiller les connaissances du modèle interactif! L'Université de technologie de Chine & meituan propose Virt, qui a à la fois l'efficacité du modèle à deux tours et la performance du modèle interacti
随机推荐
None of the strongest kings in the monitoring industry!
【Swoole系列2.1】先把Swoole跑起来
Cocos2d Lua 越来越小样本 内存游戏
Distiller les connaissances du modèle interactif! L'Université de technologie de Chine & meituan propose Virt, qui a à la fois l'efficacité du modèle à deux tours et la performance du modèle interacti
bonecp使用数据源
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
Tree-LSTM的一些理解以及DGL代码实现
Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning 论文阅读
【剑指 Offer】 60. n个骰子的点数
44所高校入选!分布式智能计算项目名单公示
Rb157-asemi rectifier bridge RB157
Epoll () whether it involves wait queue analysis
Interpreting cloud native technology
Introduction to the usage of model view delegate principal-agent mechanism in QT
阿里云国际版ECS云服务器无法登录宝塔面板控制台
Reprint: defect detection technology of industrial components based on deep learning
F200 - UAV equipped with domestic open source flight control system based on Model Design
转载:基于深度学习的工业品组件缺陷检测技术
用友OA漏洞学习——NCFindWeb 目录遍历漏洞