Solved

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

Posted on 2010-11-15
5
1,667 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now