当前位置:网站首页>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

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061017417244.html