1

I have an application that could potentially hold 600k+ records in a single WebSql table.

I have searched and searched for a way in JayData to quickly delete all of the records from a table but the only option that I have found is to actually first retrieve the records, remove them from the set, and then call saveChanges() on the set.

As you can imagine, this is extremely slow (takes minutes). I find it hard to believe that there is no way to truncate a WebSql or IndexedDb table from JayData. Am I wrong about this or is there an easier way?

Thanks.

4

2 回答 2

1

有一种方法可以实现此要求,简单的情况是,如果您想从所有表中删除所有记录并从一个干净的数据库开始。

EntityContext 的构造函数接受一个dbCreation参数:

var context = new Northwind({ name: 'webSql', databaseName: 'Northwind', dbCreation: $data.storageProviders.DbCreationType.DropAllExistingTables });

创建类型指示 JayData 不仅删除所有记录,DropAllExistingTables还删除所有表 - 并在所有上下文创建期间重新创建它。这样,您将在应用程序启动时丢失整个数据库,因此不要将此 dbCreation 指定为您的“主”上下文,而仅在需要清理数据库时创建一个新数据库 - 并将上下文实例引用设置为您的主上下文.

var context = new Nortwind({name: 'webSql', databaseName: 'Northwind'});
//... your app logic ...
var cleanContext = new Nortwind({name: 'webSql', databaseName: 'Northwind', dbCreation: $data.storageProviders.DbCreationType.DropAllExistingTables});
context = cleanContext;
// ...

如果您使用 IndexedDB,您应该在创建 cleanContext 之前关闭现有上下文:

if (context.storageProvider.db) {
    context.storageProvider.db.close();
}

如果您想保留一些数据,那么棘手的部分就来了,因为在这种情况下,您需要两个模式、两个 DB、两个上下文,并且只清理包含可删除记录的那个。

于 2013-08-21T13:58:43.867 回答
0

它们是两种方法,您可以使用本机 javascript 或 jaydata,您需要从任何控制器调用波纹管函数

 DropTable("fav_master",fav_master);

你必须在控制器中编写这个函数只有函数代码如下

方法一

        function DropTable(TableName,TableObj){
        var db = window.openDatabase("ROW_DB", "", "Cordova Demo", 200000);
        db.transaction(function (tx) {
            tx.executeSql("update sqlite_sequence set seq = 0 where name ='"+ TableName +"'");
            tx.executeSql("delete from '"+ TableName +"'");

            }); 
        }
       }

在这里,我将数据库版本设为空白,因为在创建时

方法二

       function DropTable(TableName,TableObj){
        var db = window.openDatabase("ROW_DB", "", "Cordova Demo", 200000);
        db.transaction(function (tx) {
            tx.executeSql("update sqlite_sequence set seq = 0 where name ='"+ TableName +"'");


            });

        var search;
        var promise =  TableObj.readAll();
        promise.then(function (dataset) {
            $scope.$apply(function () {
                $scope.DataSet = dataset;
                for (var x=0;x<$scope.DataSet.length;x++){
                    //console.log("Data"+$scope.DataSet[x].Id);
                    datarow=$scope.DataSet[x];
                    datarow.remove()
                    .then(function() {
                        $scope.$apply(function() {
                           var dataset = $scope.DataSet;
                           dataset.splice(dataset.indexOf(datarow), 1);
                        });
                    })
                   .fail(function(err) {
                       alert("Error deleting item");
                   });
                }
            });
        }); 
    }
于 2013-12-17T14:07:43.353 回答