tags:

views:

85

answers:

4

Hi,

Iam new to developing HTML5 applications. In this I want to insert 1000's of records into sqlite database using HTML5. This process is very slow. How to use BEGIN/COMMIT before inserting records. In this way to speed up the insertions. Please guide me anybody. Thanks in advance. Please run this example in chrome browser. This is the code for your reference:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
var msg;
//db.transaction(function(tx){tx.executeSql("BEGIN",[]);});
for(var i=0;i<1000;i++)
{
    txquer(i,"test");
}
//db.transaction(function(tx){tx.executeSql("COMMIT",[]);});
db.transaction(function (tx) {
  tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
   var len = results.rows.length, i;
   msg = "<p>Found rows: " + len + "</p>";
   document.querySelector('#status').innerHTML +=  msg;  
 }, null);
});
function txquer(i,test)
{ 
    db.transaction(
    function(tx){
      tx.executeSql('INSERT INTO LOGS (id, log) VALUES (?, ?)',[i,test]);    
    }
    );
} 
</script>
</head>
<body>
<div id="status" name="status">Status Message</div>
</body>
</html>

Regards, Neeraja.

+1  A: 

You're starting 1000 transactions. Committing a transaction is slow because it is linked to hard drive speed. See this

Benoit
I want any ideas from you.
Neeraja
Put all your `INSERT`s into the same transaction. That's all!
Benoit
I didn't get you. Please modify the above code and send it to me.
Neeraja
A: 

You already have the code to place your 1000 inserts inside a single transaction. For some reason it is commented out. Remove the comments and you are done!

db.transaction(function(tx){tx.executeSql("BEGIN",[]);});
for(var i=0;i<1000;i++)
{
    txquer(i,"test");
}
db.transaction(function(tx){tx.executeSql("COMMIT",[]);});
ravenspoint
A: 

Hi,

I put the code for BEGIN,COMMIT but no use. That's why i am commented. Remove the comments also inserting rows into database is very slow. This is the reason I asked how to speed up the process.

Neeraja
+2  A: 

This issue is that you're using a separate transaction for each step, instead of reusing it, hence why it's running poorly.

db.transaction IS a transaction, so no BEGIN/COMMIT needed.

Try this:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
var msg;
db.transaction(function (tx) {
    for(var i=0;i<1000;i++)
    {
        txquer(tx, i,"test");
    }
});
db.transaction(function (tx) {
  tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
   var len = results.rows.length, i;
   msg = "<p>Found rows: " + len + "</p>";
   document.querySelector('#status').innerHTML +=  msg;  
 }, null);
});
function txquer(tx,i,test)
{ 
    tx.executeSql('INSERT INTO LOGS (id, log) VALUES (?, ?)',[i,test]);    
} 
</script>
</head>
<body>
<div id="status" name="status">Status Message</div>
</body>
</html>
Will