Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 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