Solved

Help with array, for loop and web SQL database inserting data

Posted on 2010-11-15
5
1,684 Views
Last Modified: 2013-11-19
I have the following code but it is not inserting data from the array into the Web SQL database ... but the document.write code in the function returns the correct values - any clues?

db = openDatabase('dbnames', '1.0', 'dbnames', 4 * 1024 * 1024);
db.transaction(function (tx) {
  		tx.executeSql('CREATE TABLE IF NOT EXISTS entries1 (id unique, headname, verse, vtext)');
});
		verse1=["JoeVerse", "BobVerse", "KenVerse"];
		headname1=["JoeHead", "BobHead", "KenHead"];
		vtext1=["JoeText", "BobText", "KenText"];

for(i = 0; i < 3; i++){
eid = i + 1;
eheadname = headname1[i];
evtext = vtext1[i];
everse = verse1[i];
insertdata(eid, eheadname, evtext, everse);
}

function insertdata(eid, eheadname, evtext, everse){
db.transaction(function (tx) {
    	tx.executeSql('INSERT INTO entries1 (id, headname, verse, vtext) VALUES (?, ?, ?, ?'), [eid, eheadname, everse, evtext];
		document.write(eid + " " + eheadname + " " + evtext + " " + everse + "<br>");
});
}

Open in new window

If i skip using the array to populate the database and remove the for loop, I can use variables to populate the database (var = staticvalue) ... once I put in the for loop I can't get it to work ... HELP!!!

This is basically to populate a database on startup of the app i'm building
0
Comment
Question by:brenti
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:pg-expert
ID: 34142394
try this
tx.executeSql('INSERT INTO entries1 (id, headname, verse, vtext) VALUES (?, ?, ?, ?)', [eid, eheadname, everse, evtext];

for easy debugging, instead of executing the sql, print the sql and execute manually, this will help you fix the issue quickly

HTH
~PG
      
0
 

Author Comment

by:brenti
ID: 34142425
No, moving the ' doesn't fix it - it is supposed to be like this ?') ... doesn't create the database as it expects a ; after ...
if I use:
tx.executeSql('INSERT INTO entries1 (id, headname, verse, vtext) VALUES (?, ?, ?, ?), [eid, eheadname, everse, evtext]');
it workss but ddoesn't populate the database as well.

If I skip using the array and assign values to the variables it works using

tx.executeSql('INSERT INTO entries1 (id, headname, verse, vtext) VALUES (?, ?, ?, ?'), [eid, eheadname, everse, evtext];

So, if it works using static variables, why wouldn't it work using an array for the variables, especially if the document.write code returns the correct data??!!

how would I print the SQL and execute manually? This is run when the file loads
0
 
LVL 9

Assisted Solution

by:pg-expert
pg-expert earned 500 total points
ID: 34142507
tx.executeSql('INSERT INTO entries1 (id, headname, verse, vtext) VALUES (?, ?, ?, ?), [eid, eheadname, everse, evtext]');

instead of this, can you print it or check the logs in the database, does it throw any error ?
0
 

Accepted Solution

by:
brenti earned 0 total points
ID: 34142813
I found some other code that sshowed how to do it in a different way ... see here:
var db = openDatabase('GodsPromises3', '1.0', 'Gods Promises3', 4 * 1024 * 1024);
 
 db.transaction(
     function(tx) {
          tx.executeSql('CREATE TABLE IF NOT EXISTS entries3(id integer primary key autoincrement,headname,verse,vtext);');
     }
);

sql = "INSERT INTO entries3 (verse,headname,vtext) values (?,?,?)";
		var verse1=["JoeVerse", "BobVerse", "KenVerse"];
		var headname1=["JoeHead", "BobHead", "KenHead"];
		var vtext1=["JoeText", "BobText", "KenText"];
 db.transaction(
     function(tx) {
		 for(i = 0; i < 3; i++){
			var vals = [verse1[i],headname1[i],vtext1[i]];

          tx.executeSql(sql,vals);
		  	document.write(verse1[i] + " " + headname1[i] + " " + vtext1[i] + "<br><hr>");
			document.write(verse1[i] + " " + headname1[i] + " " + vtext1[i] + "<br><hr>");
			document.write(sql + " " + vals + "<br><hr>");
			document.write("section 2" + " " + sql + " " + vals + "<br> <hr size=\"1\" width=\"85%\">");
     }
}
);

Open in new window


Its all about placement of the for loop and something different with the insert SQL code as compared to what I was shown and what had previously worked ... the important thing, it works!!

Thanks for your help!
0
 

Author Closing Comment

by:brenti
ID: 34182386
found the correct code and syntax to make it work
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to dynamically set the form action using jQuery.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question