简体   繁体   English

Nwjs和WebSql-无法删除现有数据库?

[英]Nwjs and WebSql - can't delete existing databases?

I am creating a desktop application with Nwjs. 我正在用Nwjs创建一个桌面应用程序。 So far everything works like a charm, I really like this whole concept. 到目前为止,一切都像魅力一样运作,我真的很喜欢整个概念。

But I need database functionalities and in the Nwjs docs, WebSql is the first database on the suggested list. 但是我需要数据库功能,在Nwjs文档中,WebSql是建议列表中的第一个数据库。 I have managed to write my methods to create, insert, etc., but I can't find a way to delete databases which I have created accidently. 我已经设法编写了创建,插入等方法,但是找不到删除意外创建的数据库的方法。

This makes me wonder if WebSql is a suitable option for creating a production desktop app. 这使我想知道WebSql是否适合创建生产桌面应用程序。 Or should I switch to another database module? 还是应该切换到另一个数据库模块?

sample code: 样例代码:

var db = openDatabase('mydb', '1.0', 'my first database', 2 * 1024 * 1024);

    if(!db){
        alert('no table!');
    }

    console.log(db);

    /**** DATABASE FUNCTIONS ****/

   // Create table if not exists
    db.transaction(function (tx) {
        tx.executeSql( 'CREATE TABLE IF NOT EXISTS companies (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), street VARCHAR(128), nr VARCHAR(12), city VARCHAR(128))' );
    });


    function insertCompanyInDB(name, street, nr, city) {
        db.transaction(function (tx) {
            tx.executeSql('INSERT INTO companies (name, street, nr, city) VALUES (?, ?, ?, ?)', [name, street, nr, city]);
        });
    }

for future users, as mentioned in https://www.w3.org/TR/webdatabase/#databases https://www.w3.org/TR/webdatabase/#databases中所述,供将来的用户使用

There is no way to enumerate or delete the databases available for an origin from this API. 无法从该API枚举或删除可用于源的数据库。

but since nwjs app works as chrome extension there is away where you can delete all webSql databases (clear websql storage), like this: 但是由于nwjs应用程序作为chrome扩展程序起作用,因此您可以在此处删除所有webSql数据库(清除websql存储),如下所示:

1-in your package.json add: 1在您的package.json中添加:

"name": "myapp",
"permissions": [
    "browsingData"
  ]

2- from javascript call this command 2-从javascript调用此命令

chrome.browsingData.removeWebSQL({since:0, originTypes:{extension:true}});

Note After extensive test i personally did i recommended using LocalStorage with JSON.stringify for complex objects or SQlite for relational data structure 注意经过广泛的测试之后,我个人还是建议将JSON.stringify与LocalStorage一起用于复杂对象,或者将SQlite用于关系数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM