Jungle Jim
Jungle Jim

Reputation: 333

Trouble writing and reading SQLite database in Android

I am writing an App that will read data from an xml file on a remote server and then place it into an SQLite database on the phone/tab. I know it is successfully reading the data from the xml and placing it into the SQLite .db file. However, the problem is the rest of my app acts as though the SQLite .db file is empty. It won't access the data until I exit the App and run it again. What am I doing wrong?

Here's how I get the data:

//  Get Instrument data from XML
        while (eventType != XmlResourceParser.END_DOCUMENT) {
            if (eventType == XmlResourceParser.START_TAG) {
                //  Get the name of the tag (eg data or datum)
                String strName = instruments.getName();
                if (strName.equals("datum")) {
                    bFoundInstruments = true;
                    String datumDate = instruments.getAttributeValue(null, "date");
                    Float datumWtioil = Float.parseFloat(instruments.getAttributeValue(null, "wtioil"));
                    Float datumHh = Float.parseFloat(instruments.getAttributeValue(null, "hh"));
                    Float datumPlat = Float.parseFloat(instruments.getAttributeValue(null, "plat"));


                    publishProgress(datumDate, Float.toString(datumWtioil), Float.toString(datumHh), Float.toString(datumPlat));

                    // add data to SQLite database
                    datasource.createInstrument(datumDate, Float.toString(datumWtioil), Float.toString(datumHh), Float.toString(datumPlat));


                }
            }

Now here's how I put it into the SQLite .db file using the createInstrument() method:

public Instrument createInstrument(String instrumentDate, String instrumentWtioil, String instrumentHh, 
          String instrumentPlat) {
    ContentValues values = new ContentValues();
    //Log.d("instrumentalityWtioil",instrumentWtioil);
    values.put(ForecastSQLiteHelper.COLUMN_DATE, instrumentDate);
    values.put(ForecastSQLiteHelper.COLUMN_DATA1, instrumentWtioil);
    values.put(ForecastSQLiteHelper.COLUMN_DATA2, instrumentHh);
    values.put(ForecastSQLiteHelper.COLUMN_DATA3, instrumentPlat);
    long insertId = database.insert(ForecastSQLiteHelper.TABLE_DATA, null,
        values);
    Cursor cursor = database.query(ForecastSQLiteHelper.TABLE_DATA,
        allColumns, ForecastSQLiteHelper.COLUMN_ID + " = " + insertId, null,
        null, null, null);
    cursor.moveToFirst();
    Instrument newInstrument = cursorToInstrument(cursor);
    cursor.close();
    return newInstrument;
  }

Why isn't that data immediately available to the App? Why do I have to close the App and restart it before it's visible?

Any advice will be greatly appreciated.

Upvotes: 0

Views: 64

Answers (1)

Panayiotis Irakleous
Panayiotis Irakleous

Reputation: 2696

First of all remove the query that gets the values from the db from this function because you try to insert one instrument to db and then read it back. Do this:
a) read from xml file and add them to the db and
b) make a function that makes a query and return all the instruments from the db.

public List< Instrument > getAllInstruments() {
    // create a list
    List<Instrument> list = new ArrayList< Instrument>();

     Cursor cursor = database.query(ForecastSQLiteHelper.TABLE_DATA,
    allColumns, null, null, null, null, null);

    // check if cursor has columns
    if (cursor.getColumnCount() > 0) {
        // read all cursor values
        while (cursor.moveToNext()) {
            Instrument newInstrument = new Instrument();
            // now you have to get the values from the cursor and add them to your object
            // like this
           String instrumentName= cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
           // COLUMN_NAME is the name of the column in your database that you want the value of

          // now add the values to the Instrument object
         newInstrument.setName(instrumentName);

         // now add the object to the list
         list.add(newInstrument);
        }
    }

    cursor.close();
    return list; //finally return the list
}

Upvotes: 3

Related Questions