0

I would like to create a database file locally and write all the data into it, but would like to do it without node.js and Coffeescript. I would like to do it through Javascript just to run in the browser, as I am developing an application for storing some data and that app has to be shared with my colleagues and I don't have permission to install NodeJs in the PCs.

var sql = window.SQL;
        var db = new SQL.Database();
        db.run("CREATE TABLE test (col1, col2);");
        db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,111]);
        db.run("INSERT INTO test VALUES (?,?), (?,?)", [3,333,4,444]);
        var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
        stmt.getAsObject({$start:1, $end:1});
        stmt.bind({$start:1, $end:4});
                    while(stmt.step()) { 
            var row = stmt.getAsObject();
            console.log(row.col1)
        }

        var data = db.export();
        //As per the official documentation, the data is converted into
        //buffer and written into file using writeFileSync();
        var buffer = new Buffer(data);
        fs.writeFileSync("filename.sqlite", buffer);

But as Buffer() and writeFileSync() are NodeJs functions, I can't use them in my code. Is there any other way I could write my data into the database and then export it into a file?

2
  • Do you have a central server for your app, on which there is the database available? Commented Aug 9, 2016 at 12:58
  • No, I am developing this app to store our personal information in our PC. So I am thinking of putting the database file locally and retrieve the data from it. Commented Aug 9, 2016 at 14:33

1 Answer 1

0

It's not good idea to execute a requests to database from your client code. It's not secure. And you basically need for node on your PC because it's a runtime environment of JS and Buffer with writeFileSync() from fs module is a part of this runtime.

Sign up to request clarification or add additional context in comments.

3 Comments

I do understand that, its not secure, but I am not going to deploy the app in any server. Its just for me and my colleagues, personal requirements to store our data on to our local PCs.
@SaiRamMallikCheripally All the same you cannot do this. JavaScript doesn't have to access to file system by design. If you want to work with FS and/or DB you should write a backend server or a standalone desktop application.
Apart from the Official Documentation example for the sql.js, could you please provide any working links for the sql.js usage in Javascript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.