electron+sqlite3单机应用安装

本文介绍了在VueCLI构建的Electron应用中,如何安装并使用SQLite3数据库,包括在background.js中创建数据库连接,以及在App.vue中通过IPC通信查询数据的过程。开发者在开发和打包阶段都能与数据库交互,但需注意测试生产环境的兼容性。
摘要由CSDN通过智能技术生成

网上看到很多需要使用python和vs2015的教程,我很奇怪的是,我在一台电脑上装了,另一台电脑上没有装
但是在开发模式下都可以对数据库进行操作,这让我很迷惑,因为我还没有测试打包之后的数据库是否可以正常交互!!! 有electron大佬求指教
在上一篇vuecli+electron安装教程中讲述了如何安装electron
下面将针对sqlite3的测试使用整理一下文档
安装sqlite3

cnpm i  sqlite3

在目录dist_electron的目录中创建了一个config文件夹里面放了一个数据库文件
在这里插入图片描述
在background.js中createWindow中加了一个测试链接的方法
在这里插入图片描述

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


async function createWindow() {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
    }
  })

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
    if (!process.env.IS_TEST) win.webContents.openDevTools()
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
  }
  const dbPath = path.join(__dirname, './config/sakwPro.db');
  const db = new sqlite3.Database(dbPath, (err) => {
      if (err) {
          console.error('Error opening database', err);
      } else {
          console.log('Successfully connected to the database',dbPath);
      }
  });
  ipcMain.on('query-database', (event, query) => {
      db.all(query, [], (err, rows) => {
          if (err) {
              throw err;
          }
          event.reply('query-reply', rows);
      });
  });
}

App.vue中加了一个按钮,去查询case表中的数据


<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <button @click="testDb">测试</button>
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'
// import {createDataTable} from './db/database.ts';
import { ipcRenderer } from 'electron';



export default {
  name: 'App',
  components: {
    HelloWorld
  },data(){
    return{}
  },
  methods:{
    testDb(){
    ipcRenderer.send('query-database', 'SELECT * FROM `case`');

   ipcRenderer.on('query-reply', (event, data) => {
      console.log(data); // 处理数据库查询结果
    });
        
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

vue.config.js中配置一下path

// eslint-disable-next-line no-unused-vars
const path = require('path');

module.exports = {
  configureWebpack: {
    resolve: {
      alias: {
        // 如果还有其他需要别名的模块,可以在这里添加
      },
      fallback: {
        "path": require.resolve("path-browserify"),
        "fs": false  // 如果你不需要 'fs' 模块,可以设置为 false
      }
    }
  },
  pluginOptions: {
    electronBuilder: {
      // Electron Builder 的配置项
      nodeIntegration: true
    }
  }
};

npm run electron:serve运行项目,查询到了案件表
在这里插入图片描述

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值