jeudi 3 septembre 2015

Using sql.js in an Electron application

I am new to Electron. It's pure awesome, and getting started is super fun and easy. For some time now I have been trying to find a "database" solution to use in my application. Of course there is the Web SQL/local storage options, but I am trying to use SQLite. I found sql.js, which is great and easy to use. I could get everything to run correctly put I cannot save/update the database file! Here is the code:

    var remote = require('remote'),
    fileSystem = remote.require('fs'),
    sql = remote.require('./nodeModules/sql.js'),
    database = new sql.Database(fileSystem.readFileSync('./database.sqlite'));

    database.run('CREATE TABLE IF NOT EXISTS products (ID integer primary key autoincrement, name text, price integer, stock integer)');

    // Save the data back to the file
    var data = database.export();
    var buffer = new Buffer(data);
    fileSystem.writeFileSync("./database.sqlite", buffer);

But I am getting this error from "Buffer": Uncaught TypeError: must start with number, buffer, array or string. Have you run across this issue before ?

Aucun commentaire:

Enregistrer un commentaire