4

I scrap data on the web with a Google Chrome extension. I store them in an multidimensional array. I want to save all theses data in a Sqlite database.

I read this page How to speed up the process when inserting 1000's of records into sqlite using HTML5 but the answer given does not seem to work.

When I open a transaction for each INSERT it works

i=0;
while(i<n){
    (function(aa){
        db.transaction(function (tx) {
             tx.executeSql("INSERT INTO Links2 (c1, c2, c3, c4, c5, c6) VALUES ('"+aa[0]+"', "+parseInt(aa[1])+", '"+parseInt(aa[2])+"', '"+aa[3]+"', '"+aa[4]+"', '"+aa[5]+"')");
        });
    })(myDataArray[i]);    
    i++;
}

The problem is when I try to opening the transaction before the While

db.transaction(function (tx) {
    i=0;
    while(i<n){
        aa = myDataArray[i];
        txquer(tx, i, aa[0], parseInt(aa[1]), parseInt(aa[2]), aa[3], aa[4], aa[5]);
        i++;
    }
});
function txquer(tx,i,a,b,c,d,e,f){  
    console.log("INSERT INTO Links2 (c1, c2, c3, c4, c5, c6) VALUES ('"+a+"', "+b+", '"+c+"', '"+d+"', '"+e+"', '"+f+"')");
    tx.executeSql("INSERT INTO Links2 (c1, c2, c3, c4, c5, c6) VALUES ('"+a+"', "+b+", '"+c+"', '"+d+"', '"+e+"', '"+f+"')");
} 

When I test individually every console.log output I works. But the script does not save the data in the database.

4

2 回答 2

1

Each of the tx.executeSql is executed asynchronously, so the while loop will be already over when executing each query, i == n and aa == myDataArray[i-1].

[EDIT]

You should keep the call to tx.executeSql inside the closure to avoid aa being overriden:

db.transaction(function (tx) {
    i=0;
    while(i<n){
        (function(aa){
            txquer(tx, i, aa[0], parseInt(aa[1]), parseInt(aa[2]), aa[3], aa[4], aa[5]);
        })(myDataArray[i]);
        i++;
    }
});

function txquer(tx,i,a,b,c,d,e,f){  
    console.log("INSERT INTO Links2 (c1, c2, c3, c4, c5, c6) VALUES ('"+a+"', "+b+", '"+c+"', '"+d+"', '"+e+"', '"+f+"')");
    tx.executeSql("INSERT INTO Links2 (c1, c2, c3, c4, c5, c6) VALUES ('"+a+"', "+b+", '"+c+"', '"+d+"', '"+e+"', '"+f+"')");
}

Couldn't test this code so I'm not sure it's working but I hope you can get the idea.

于 2013-06-27T10:20:50.117 回答
0

I've found a solution! It is maby not the best way to do this but it is much faster than opening each time a transaction.

The main idea is to execute the the next executeSql when the previous one is done. I implemented a recursive function.

n=mainDataArray.length;
console.log(mainDataArray);
//Saving the array in the db
db.transaction(function(tx) {   
    saveData = function(dataArray,k){
        var aa = dataArray[k];
        console.log(aa);
        tx.executeSql("INSERT INTO Links2 (c1, c2, c3, c4, c5, c6) VALUES ('"+aa[0]+"', "+parseInt(aa[1])+", '"+parseInt(aa[2])+"', '"+aa[3]+"', '"+aa[4]+"', '"+aa[5]+"')", [], 
        function (tx, callback) {
            console.log("ok "+dataArray.length+" | k="+k);
            if(k<(dataArray.length-1)){
               saveData(dataArray,(k+1));
            }
        },
        function (tx, error) {
            console.log("error "+dataArray.length+" | k="+k);
            if(k<(dataArray.length-1)){
               saveData(dataArray,(k+1));
            }
        });
    }
    saveData(mainDataArray,0);
});

You can find more information on this website Saving an Array into a Sqlite database with javascript

If you find a better solution please post-it here :)

于 2013-06-27T12:12:15.787 回答