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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With