如何使用没有NodeJs和Coffeescript的sql.js创建someFileName.sqlite数据库,但只使用Javascript并使用它?

时间:2022-03-22 15:50:25

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.

我想在本地创建一个数据库文件并将所有数据写入其中,但是如果没有node.js和Coffeescript,我想这样做。我想通过Javascript只是为了在浏览器中运行,因为我正在开发一个用于存储一些数据的应用程序,并且该应用程序必须与我的同事共享,而我无权在PC中安装NodeJ。

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?

但由于Buffer()和writeFileSync()是NodeJs函数,我不能在我的代码中使用它们。有没有其他方法可以将我的数据写入数据库然后将其导出到文件中?

1 个解决方案

#1


1  

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.

从客户端代码执行对数据库的请求并不是一个好主意。这不安全。你基本上需要你的PC上的节点,因为它是JS和缓冲区的运行时环境,来自fs模块的writeFileSync()是这个运行时的一部分。

#1


1  

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.

从客户端代码执行对数据库的请求并不是一个好主意。这不安全。你基本上需要你的PC上的节点,因为它是JS和缓冲区的运行时环境,来自fs模块的writeFileSync()是这个运行时的一部分。