?
Solved

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

Posted on 2010-11-15
5
Medium Priority
?
1,760 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

719 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