0

Hi I tried the following code, and give me the list of records in the table properly in a list view as output.

but when I comment the following 2 methods which are in side the Main_Activity class, the database records do not get loaded. createTable(); insertData();

I was wondering whether have to create table and insert same record set again when we open the same app after shut down it.

Does anyone have idea of why it happens so... Thank you..

public class Main_Activity extends ListActivity {

private static String SAMPLE_TABLE_NAME = "PERSONS_TABLE";
private SQLiteDatabase sampleDB = null;
private List<String> results = new ArrayList<String>();
private Cursor cursor = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

    try
    {
        sampleDB = openOrCreateDatabase("NAME", MODE_PRIVATE, null);
        createTable();
        insertData();
        lookupData();
        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
    }
    catch (SQLiteException se)
    {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    }
    finally
    {

        if (sampleDB != null)
            sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
        sampleDB.close();
    }

}

private void createTable()
{
   sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                SAMPLE_TABLE_NAME +
                " (PERSON_NAME VARCHAR, " +
                "  COUNTRY VARCHAR, " +
                "  AGE INT(3));");
}

private void insertData()
{
    sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('James','ENGLAND',25);");
    sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Dave','USA',18);");
    sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Jean-Paul','FRANCE',33);");
    sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Sergio','SPAIN',42);");
    sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values ('Hitori','JAPAN',73);");
}

private void lookupData()
{
    cursor = sampleDB.rawQuery("SELECT PERSON_NAME, COUNTRY, AGE FROM " +
            SAMPLE_TABLE_NAME +
            " where AGE > 10 ", null);

    if (cursor != null)
    {
        if (cursor.moveToFirst())
        {
            do
            {
                String personName =     cursor.getString(cursor.getColumnIndex("PERSON_NAME"));
                String country = cursor.getString(cursor.getColumnIndex("COUNTRY"));
                int age = cursor.getInt(cursor.getColumnIndex("AGE"));
                results.add("" + personName + ", " + country + ", " + age);
            } 
            while (cursor.moveToNext());
        }
        cursor.close();
    }
}
}

1 Answer 1

1

In your try statement you're creating DB, populate it... and then in finally statement you delete all records. That's why your DB always empty. You should remember that finally executes in any case after try or catch.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.