Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to speed up the process when inserting 1000's of records into sqlite using HTML5

Tags:

html

sqlite

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.

like image 482
Neeraja Avatar asked Oct 14 '10 06:10

Neeraja


1 Answers

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>
like image 163
Will Avatar answered Sep 22 '22 14:09

Will