當前位置:成語大全網 - 新華字典 - 在electron中使用sqlite3,win10系統

在electron中使用sqlite3,win10系統

1>安裝python(推薦2.7版本),切記將python加入系統環境變量

2>安裝sqlite3,推薦使用npm安裝,不要使用cnpm,cnpm安裝的文件存在問題(npm install sqlite3 --save)

3>在package.json中加入"rebuild": "electron-rebuild -f -w sqlite3"

4>在命令行執行npm run rebuild,重新編譯sqlite3,執行成功後就可以在electron中使用sqlite3

1>在根目錄下生成db文件

const sqlite3 = require('sqlite3').verbose()

const db =new sqlite3.Database('info.db', function() {

? db.run('create table test(name varchar(15))', function () {

db.run('insert into test values("hello,world")', function () {

? db.all('select * from test', function(err, res) {

? if (!err) {

console.log(JSON.stringify(res))

? }else {

console.log(err)

? }

})

})

})

})

2>在指定的文件夾裏使用(data文件夾需手動創建)

const sqlite3 = require('sqlite3').verbose()

const path = require('path')

const db =new sqlite3.Database(path.join(__dirname, '../data/info.db'))

? db.run('create table test(name varchar(15))', function () {

db.run('insert into test values("hello,world")', function () {

db.all('select * from test', function(err, res) {

? if (!err) {

? console.log(JSON.stringify(res))

}else {

console.log(err)

? }

})

})

})