当前位置:网站首页>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 worker
Right 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
边栏推荐
- Interpreting cloud native technology
- Codeforces Round #803 (Div. 2)
- Alibaba cloud international ECS cannot log in to the pagoda panel console
- STM32+ESP8266+MQTT协议连接OneNet物联网平台
- STM32+MFRC522完成IC卡号读取、密码修改、数据读写
- Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
- Recursive way
- C language college laboratory reservation registration system
- SAP Fiori 应用索引大全工具和 SAP Fiori Tools 的使用介绍
- 287. Find duplicates
猜你喜欢
30 分钟看懂 PCA 主成分分析
MS-TCT:Inria&SBU提出用于动作检测的多尺度时间Transformer,效果SOTA!已开源!(CVPR2022)...
30 minutes to understand PCA principal component analysis
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
【Swoole系列2.1】先把Swoole跑起来
Xu Xiang's wife Ying Ying responded to the "stock review": she wrote it!
UDP protocol: simple because of good nature, it is inevitable to encounter "city can play"
10、 Process management
[the 300th weekly match of leetcode]
简单易用的PDF转SVG程序
随机推荐
Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning 论文阅读
C语言自动预订飞机票问题
Penetration test information collection - CDN bypass
Splay
C language exchanges two numbers through pointers
2019阿里集群数据集使用总结
2022 Summer Project Training (I)
Tree-LSTM的一些理解以及DGL代码实现
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
CSRF vulnerability analysis
C language college laboratory reservation registration system
Windows连接Linux上安装的Redis
UDP协议:因性善而简单,难免碰到“城会玩”
推荐好用的后台管理脚手架,人人开源
Jerry is the custom background specified by the currently used dial enable [chapter]
Echart simple component packaging
Use cpolar to build a business website (1)
Picture zoom Center
Maixll dock camera usage
监控界的最强王者,没有之一!