当前位置:网站首页>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
边栏推荐
- Bonecp uses data sources
- epoll()无论涉及wait队列分析
- 2022 Summer Project Training (II)
- Why does wechat use SQLite to save chat records?
- A method of sequentially loading Unity Resources
- None of the strongest kings in the monitoring industry!
- Compilation Principle -- C language implementation of prediction table
- Windows连接Linux上安装的Redis
- 推荐好用的后台管理脚手架,人人开源
- Prophet模型的简介以及案例分析
猜你喜欢
C language exchanges two numbers through pointers
阿里云国际版ECS云服务器无法登录宝塔面板控制台
TOP命令详解
推荐好用的后台管理脚手架,人人开源
On time and parameter selection of asemi rectifier bridge db207
Top command details
287. 寻找重复数
Grafana 9.0 is officially released! It's the strongest!
图之广度优先遍历
Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning 论文阅读
随机推荐
Markdown grammar - better blogging
2022 Summer Project Training (II)
TOP命令详解
atcoder它A Mountaineer
2022 Summer Project Training (III)
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
测试1234
传输层 拥塞控制-慢开始和拥塞避免 快重传 快恢复
【中山大学】考研初试复试资料分享
[Android] kotlin code writing standardization document
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
Virtual machine VirtualBox and vagrant installation
Cobra 快速入门 - 专为命令行程序而生
Numerical analysis: least squares and ridge regression (pytoch Implementation)
Easy to use PDF to SVG program
Wchars, coding, standards and portability - wchars, encodings, standards and portability
当保存参数使用结构体时必备的开发技巧方式
epoll()无论涉及wait队列分析
MySQL查询请求的执行过程——底层原理
【Swoole系列2.1】先把Swoole跑起来