Database being overwritten ( have to open the app twice to display correctly )?
<p>LATEST: check code at the bottom for my hack solution..</p>

<p>ORIGINAL: I've followed some tutorials and examples and went and made my own database.  The problem is that when I download the database to the phone it's supposed to be read and fill a spinner.. i've found that the downloaded database is intact but when it gets read by the dbhelper it somehow gets rewritten into a blank table.  I can't understand why.</p>

<p>EDIT1:
I'm sorry, I should have been more clear. The database is coming from a server. it is downloaded directly to /data/data/project/databases/ . That's where I checked the file after it was downloaded. It was good until I called 'DbAdapter mDbHelper = new DbAdapter(context); mDbHelper.open("names");' after which the Android Metadata table and names table and sqlite table are still in the database, the columns have names, but there are no entries so the table is essentially blank. </p>

<p>EDIT2: (sorry so lengthy)</p>

<p>I commented back in the following:        </p>

<pre><code>c = initSpinner(c);
</code></pre>

<p>it's a method I wrote in case the cursor returned ! moveToFirst() . in this method the database is created and "add name" is inserted into the table. </p>

<p>I then played with the app a little and this is what I found:</p>

<p>when i first open the app on my device, on a clean install, the spinner shows the default "add a name". so i add a name.. I reopen the app and the name i entered is there along with "add a name"... go to the emulator after clearing all the data and uninstalling.. open the app and only "add a name" is there.. back out and reopen and the spinner has "add a name" and the name i entered on my phone... so i add a name on the emulator, go back to my phone and close and reopen the app and there are now 2 names and "add a name"... </p>

<p>Summary: It works but I have to reopen the app after the initial install to read the db and populate the spinner correctly. Why?</p>

<p>EDIT3 (latest): see code at the end for the hack solution..</p>

<p>here is the code that fills the spinner.</p>

<pre><code>    private void fillSpinner(){

    DbAdapter mDbHelper = new DbAdapter(context);
    mDbHelper.open("names");

    Cursor c = mDbHelper.fetchAllRows();
    ArrayList&lt;String&gt; a = new ArrayList&lt;String&gt;();

    if ( c.getPosition() != 0 &amp;&amp; c.moveToFirst() == (false) ){
//          c = initSpinner(c); //commented out to prevent creating a new     //database.
    }
</code></pre>

<p>here is the DbAdapter code.</p>

<pre><code>public class DbAdapter {

// Database fields
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";

static final String DATABASE_TABLE = "names";
private Context context;
private SQLiteDatabase database;
private DbHelper dbHelper;


public DbAdapter(Context ctx) {
    context = ctx;

}

public SQLiteDatabase openToRead() throws SQLException {
    dbHelper = new DbHelper(context);
    database = dbHelper.getReadableDatabase();

    return database;
}

public SQLiteDatabase open(String dbname) throws SQLException {
    dbHelper = new DbHelper(context);
    database = dbHelper.getWritableDatabase();
    return database;
}

public void close() {
    dbHelper.close();
}

//  
/**
 * Create a new todo If the todo is successfully created return the new
 * rowId for that note, otherwise return a -1 to indicate failure.
 */

public long createRow(String name) {
    ContentValues initialValues = createContentValues(name);
    return database.insert(DATABASE_TABLE, null, initialValues);
}

public long createRowWithAL(ArrayList&lt;String&gt; values){

    ContentValues v = ArrayListToContentValues(values);

    return database.insert(DATABASE_TABLE, null, v);

}

private ContentValues ArrayListToContentValues(ArrayList&lt;String&gt; parcel){

    ContentValues values = new ContentValues();

//      values.put(KEY_ROWID, _id);
    values.put(KEY_NAME, parcel.get(0));
    return values;
}



/**
 * Update the todo
 */

public boolean updateRows(long rowId, 
        String name) {

    ContentValues updateValues = createContentValues(
            name);

    return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "="
            + rowId, null) &gt; 0;
}


/**
 * Deletes todo
 */

public boolean deleteRow(long rowId) {
    return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) &gt; 0;
}


/**
 * Return a Cursor over the list of all todo in the database
 * 
 * @return Cursor over all notes
 */

public Cursor fetchAllRows() {
    return database.query(DATABASE_TABLE, null, null, null, null,
            null, null);
}


/**
 * Return a Cursor positioned at the defined todo
 */

public Cursor fetchRow(long rowId) throws SQLException {
    Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
            KEY_ROWID ,KEY_NAME},
            KEY_ROWID + "=" + rowId, null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}


private ContentValues createContentValues(String name){

    ContentValues values = new ContentValues();

    values.put(KEY_NAME, name);
    return values;
}

//returns (an) entire column(s), all rows
public Cursor fetchColumns(String[] colnames) {
    Cursor mCursor = database.query(DATABASE_TABLE, colnames, null, 
            null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}
</code></pre>

<p>}</p>

<p>Here is the DbHelper code.</p>

<pre><code>public class DbHelper extends SQLiteOpenHelper {

Context context;

private static final String DATABASE_NAME = "names";
private static final int DATABASE_VERSION = 1;

// Database creation sql statement
private static final String DATABASE_CREATE = "create table if not exists "+DATABASE_NAME+" (_id integer primary key autoincrement, " +
    "name text not null);";

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

// Method is called during creation of the database
@Override
public void onCreate(SQLiteDatabase database) {

    database.execSQL(DATABASE_CREATE);
}

// Method is called during an upgrade of the database, e.g. if you increase
// the database version
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
        int newVersion) {
    Log.w(DbHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
    database.execSQL("DROP TABLE IF EXISTS names");
    onCreate(database);
</code></pre>

<p>}
}</p>

<p>---HACK CODE:</p>

<pre><code>    private void fillSpinner(){

    DbAdapter mDbHelper = new DbAdapter(context);
    mDbHelper.open("name");
        mDbHelper.createRow("Add Name"); //added this
    mDbHelper.close();               //added this
    downloadFile();                  //added this
    mDbHelper.open("names");

    Cursor c = mDbHelper.fetchAllRows();
    ArrayList&lt;String&gt; a = new ArrayList&lt;String&gt;();

    if ( c.getPosition() != 0 &amp;&amp; c.moveToFirst() == (false) ){
        c.close();          

        mDbHelper.createRow("Add Name");
        c = mDbHelper.fetchColumns(new String[] {"_id","name"});
    }
</code></pre>
