PeakGen
PeakGen

Reputation: 23035

Unable to retrieve data from SQLite

Please have a look at the following code

DatabaseHandler.java

The code used to retrieve data

public List<String> getAllBranches() {
        // TODO Auto-generated method stub

        String selectQuery = "select city from BranchNetwork";

        Cursor cursor = database.rawQuery(selectQuery, null);
        List <String>branches = new ArrayList<String>();

        if(cursor.isFirst())
        {
            do
            {

            }
            while(cursor.moveToNext());

            Toast.makeText(context, "Data Retrieved: "+branches.get(1), Toast.LENGTH_LONG).show();
        }

        return branches;
    }

The code used to insert data

public String insertData(String city, String streetAddress,String phoneNumber1, String phoneNumber2, String email) 
    {
        String insertQuery = "insert into BranchNetwork ('city','streetAddress','phoneNumber1','phoneNumber2','email') values('"+city+"','"+streetAddress+"','"+phoneNumber1+"','"+phoneNumber2+"','"+email+"');";




        try
        {  
            database.execSQL(insertQuery);


            return "Data Successfully Inserted";

        }
        catch(Exception e)
        {
            Toast.makeText(context, "Exception: "+e.getMessage(), Toast.LENGTH_LONG).show();
            return "Data Insertion Failed";
        }

    }

Form.java

The first Activity. Here I retrieve the data and insert the data. Following is how I retrieve the data

public class Form extends Activity {
    private List<String>branches = new ArrayList<String>();
        private DatabaseConnector databaseConnector;
    String[]arr;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_form);

            databaseConnector = DatabaseHandler.getInstance(this);
            databaseConnector.createConnection();

            branches = databaseConnector.getAllBranches();

             arr = branches.toArray(new String[branches.size()]);
    }
}

But, I never get anything! In DatabaseHandler.java, I have made a Toast where it should generate a message when the code is retrieved. But it is not generating anything! I am loading the data into a ListView but it is also empty! Why is this? Please help!

PS: Following is the DatabaseConnector.java, the insert face for DatabaseHandler

package com.example.esoftcallmanager;

import java.util.List;

import android.content.Context;

public interface DatabaseConnector 
{
    public void createConnection();
    public void closeConnection();
    public String getPhoneNumber();
    public String insertData(String city, String streetAddress, String phoneNumber1, String phoneNumber2, String email);
    public List<String>getAllBranches();

}

Upvotes: 0

Views: 174

Answers (5)

GrIsHu
GrIsHu

Reputation: 23638

You forget to add the data in your array of list. Try out as below:

  String selectQuery = "select city from BranchNetwork";
    Cursor cursor = database.rawQuery(selectQuery, null);
    List <String> branches = new ArrayList<String>();
    String str=null;
    cursor.moveToFirst();
        do
       {
         str= m_cursor.getString(m_cursor.getColumnIndex("city"));
          branches.add(str);
       }
        while(cursor.moveToNext());
        Toast.makeText(context, "Data Retrieved: "+branches.get(1), Toast.LENGTH_LONG).show();
    }
        return branches;

Upvotes: 0

Manmeet Singh Batra
Manmeet Singh Batra

Reputation: 467

Hope this works.

public List<String> getAllBranches() {
            // TODO Auto-generated method stub

            String selectQuery = "select city from BranchNetwork";
            Cursor cursor = database.rawQuery(selectQuery, null);
            List <String>branches = new ArrayList<String>();

             if(cursor != null){
              cursor.moveToFirst();

               Toast.makeText(context, "Data Retrieved: ", Toast.LENGTH_LONG).show();

              do
                {

                }
                while(cursor.moveToNext());



             }



                return branches;
        }

Upvotes: 1

Stefan de Bruijn
Stefan de Bruijn

Reputation: 6319

That's because you're not really doing anything with the data.

First of all you have to move the cursor's position to first yourself: cursor.moveToFirst() After that you can start a while loop as you're trying to do.

Secondly, you never actually put the data in your List branches, you'll have to place the data from the cursor in there first.

Upvotes: 1

Kapil Vats
Kapil Vats

Reputation: 5515

try cursor.moveToFirst() Condition

if(cursor.moveToFirst())
    {
        do
        {

        }
        while(cursor.moveToNext());

        Toast.makeText(context, "Data Retrieved: "+branches.get(1), Toast.LENGTH_LONG).show();
    }

Upvotes: 1

iTech
iTech

Reputation: 18460

If you are sure that your database has data already, try to change if(cursor.isFirst()) to if(cursor.moveToFirst())

Upvotes: 2

Related Questions