Following the indications found here: Sqlite Error: file is not a database I double-checked that the path does not contain .db
extension:
export const getInfopiecesDbpath = () {
const isEnvDevelopment = process.env.NODE_ENV === 'development'
const infopiecesDbpath = isEnvDevelopment
? path.join(app.getAppPath(), "src", "data", "infopieces")
: path.join(app.getAppPath(), ".webpack", "data", "infopieces")
return infopiecesDbpath
}
const createInfopiecesTable: any = `
CREATE TABLE IF NOT EXISTS infopieces (
id INTEGER PRIMARY KEY AUTOINCREMENT,
infotext TEXT NOT NULL
);`
const createInfopiecesIndex = `PRAGMA INDEX_LIST(infopieces);`
export function initializeInfopiecesDB(db: Database) {
db.prepare(createInfopiecesTable).run()
db.prepare(createInfopiecesIndex).get()
}
const insertIP = `
INSERT INTO infopiecesDb (id, infotext)
VALUES (ip.id, ip.infotext)
`
export const insertInfopiece = (ip: IInfopiece) => {
infopiecesDb.prepare(insertIP).run(ip)
}
let infopiecesDbpath = getInfopiecesDbpath()
let infopiecesDb = require('better-sqlite3-multiple-ciphers')(infopiecesDbpath, { verbose: console.log })
infopiecesDb.pragma("key='secret-key'")
initializeInfopiecesDB(infopiecesDb)
in main
:
let infopiecesDbpath = getInfopiecesDbpath()
let infopiecesDb = require('better-sqlite3-multiple-ciphers')(infopiecesDbpath, { verbose: console.log })
infopiecesDb.pragma("key='secret-key'")
initializeInfopiecesDB(infopiecesDb)
ipcMain.handle ("insert-infopiece-intodb", (IpcMainEvent, message) => {
console.log("ipcMain.hanlde-insert-infopiece-intodb-message: ", message.ip_01)
insertInfopiece(message.ip_01)
})
I get this error:
Error occurred in handler for 'insert-infopiece-intodb': SqliteError: file is not a database
at Database.prepare (/home/raphy/Playground/.webpack/main/index.js:652:21)
at insertInfopiece (/home/raphy/Playground/.webpack/main/index.js:1773:18)
at /home/raphy/Playground/.webpack/main/index.js:1943:32
at node:electron/js2c/browser_init:193:579
at EventEmitter.<anonymous> (node:electron/js2c/browser_init:161:10433)
at EventEmitter.emit (node:events:390:28) {
code: 'SQLITE_NOTADB'
}
Other info:
"@types/better-sqlite3": "^7.5.0",
"better-sqlite3": "^7.5.3",
"better-sqlite3-multiple-ciphers": "^7.5.2",
"electron": "17"
node: v16.15.0
What am I missing and / or doing wrongly?
Update 1)
If I put in main
:
const infopiecesDb = require('better-sqlite3-multiple-ciphers')("../data/infopieces.sqlite", {timeout: 5000}) infopiecesDb.exec(`CREATE TABLE IF NOT EXISTS infopieces ( id INTEGER PRIMARY KEY AUTOINCREMENT, infotext TEXT NOT NULL, doctype VARCHAR(255), dateStamp DATETIME NOT NULL, shelfLife DATETIME NOT NULL, source VARCHAR(255) NOT NULL, lineage TEXT, reliability INTEGER, person VARCHAR(255), company VARCHAR(255), privacy INTEGER NOT NULL, authorization TEXT, tag VARCHAR(255) );')
infopiecesDb.pragma("key='secret-key'")
I get SqliteError: unable to open database file
error
Try this: