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

Posted on 2010-11-15
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
Question by:brenti
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

Expert Comment

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


Author Comment

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

Assisted Solution

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 ?

Accepted Solution

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);
     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"];
     function(tx) {
		 for(i = 0; i < 3; i++){
			var vals = [verse1[i],headname1[i],vtext1[i]];

		  	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!

Author Closing Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Div not showing up 6 33
type of website 13 54
How do I Evaluate an expression using OR in jquery 16 19
Today Date 3 13
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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.
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…

710 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