0

I need to fill an array with each of the values ​​for "name" and "nickname" taken from a SQLITE database and show it in an alert.

This is for a project written in JavaScript in titanium appcelerator.

This is the code:

var fightersRS = db.execute('SELECT * FROM fighters');

 var setFighters = [];

while (fightersRS.isValidRow())
{
  var name = fightersRS.fieldByName('name');
  var nickname = fightersRS.fieldByName('nickname');

      setFighters.push = {
             name: name,
             nickname: nickname           
             };

     fightersRS.next();
}
alert(setFighters);

The idea is that the alert displays all comma separated values, for example:

"muhammad ali, rocky balboa, ten shin han, etc ...."

How I can do it? my code does not work.

Nacho Sarmiento
  • 463
  • 3
  • 8
  • 17

2 Answers2

1

Change your code as follows

var fightersRS = db.execute('SELECT * FROM fighters');
var setFighters = [];
while (fightersRS.isValidRow())
{
  var name = fightersRS.fieldByName('name');
  var nickname = fightersRS.fieldByName('nickname');

      setFighters.push({"name": name, "nickname": nickname}); //This is the correct syntax for pushing an element to the array

     fightersRS.next();
}

You can display the elements using a for loop as follows

for(var i=0; i <setFighters.length;i++){
    alert("Name = " + setFighters[index].name + " Nickname= " + setFighters[index].nickname);
}

If you want to display their name with comma, try the following

var fightersRS = db.execute('SELECT * FROM fighters');
var setFighters = [];
while (fightersRS.isValidRow())
{
    var name = fightersRS.fieldByName('name');

    setFighters.push(name);

    fightersRS.next();
}

var fighters = setFighters.join(",");//Joining the array elements with comma
alert(fighters); //Will display all names with a comma

Look at javascript join method

Anand
  • 5,323
  • 5
  • 44
  • 58
-1

Use a for loop or inside the while loop only create a string appending all the values with comma.

Fetching all the data from an array with comma is not possible. Here's an asccociative array example from stack over flow .. you can have your own for loop or use the while loop you are creating

Associative array and fetching data

Thanks

Community
  • 1
  • 1
lopa
  • 474
  • 1
  • 3
  • 14