Get rid of the numbers and quotes in the text file, if you can, they should not be needed (your counter will do this for you).
Change the code to read the text file:
var file = "file/words.txt"; function getFile(){ $.get(file, function(txt){ var lines = txt.responseText.split("\n"); for (var i = 0, len = lines.length; i < len; i++) { save(lines[i]); } }); }
Now you have an array with each line of your file; if you have deleted numbers and quotation marks, these should only be car names.
for (var i = 0; i < lines.length; i++) { tx.executeSql('INSERT INTO Car (id, name) VALUES (i, lines[i]); }
If you want to save the file as is, change the line below:
var lines = txt.responseText.split(":");
Now the array contains the number and name of the car (odd number, even the name of the car). We can get rid of double quotes (SQL may cause an error on them):
lines.replace(/"/g, '').trim(); for (var i = 0; i < lines.length; i++) { tx.executeSql('INSERT INTO Car (id, name) VALUES (i, lines[i+1])'); i++;
source share