网上看到很多需要使用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运行项目,查询到了案件表