No such table even though db.execSQL(sql) executes without an error

admin

Administrator
Staff member
I have a weird issue to work through. The system properly calls the SQLiteOpenHelper onCreate() method and it also seems that <strong>db.execSQL(sql)</strong> is successful, but the table is never created.

I'm accessing the VM using "adb devices" >> cd into the /data/data/package/databases >> sqlite3 to run .tables. If I execute "select * from posts" the result is "No such table." Thanks for any guidance.

<strong>EDITED</strong>

I added the MainActivity class to show that I do call .getWritableDatabase(). When I do I can see that the onCreate method is called and there aren't any errors, but when I check to see if the table is there... it isn't.

<strong>DatabaseHelper.java</strong>

Code:
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String TAG = "DnDa";

    public static final String DATABASE_NAME = "db-rss.db";
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_TABLE = "posts";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_TITLE = "title";
    public static final String COLUMN_LINK = "link";
    public static final String COLUMN_DESCRIPTION = "description";
    public static final String COLUMN_PUBDATE = "pubdate";
    public static final String COLUMN_CATEGORY = "category";
    public static final String COLUMN_GUID = "guid";

    private Post post;
    private ArrayList&lt;Post&gt; posts;
    private Cursor cursor;

    /**
     * constructor
     * @param context
     */
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) throws SQLiteException {
        String sql = String.format("CREATE TABLE %s ( %s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT )",
                DATABASE_TABLE, COLUMN_ID, COLUMN_TITLE, COLUMN_LINK, COLUMN_DESCRIPTION, COLUMN_PUBDATE, COLUMN_CATEGORY, COLUMN_GUID);

        try {
            db.execSQL(sql);
            Log.d(TAG, "Successfully created table: " + sql);
        } catch (SQLiteException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS" + DATABASE_TABLE;
        db.execSQL(sql);

        this.onCreate(db);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        Log.d(TAG, "Opening database");
    }

    /**
     * Retrieve all records
     */
    public Cursor getAllRecords(SQLiteDatabase db) {
        db = this.getReadableDatabase();
        cursor = db.query(DATABASE_TABLE, new String[] {COLUMN_TITLE, COLUMN_PUBDATE, COLUMN_DESCRIPTION}, null, null, null, null, null);
        return cursor;
    }

}

<strong>MainActivity.java</strong>

Code:
public class MainActivity extends ActionBarActivity {
    private static final String TAG = "DnDa";

    private String finalUri = "http://wordpress.com/feed/";
    private DatabaseHelper databaseHelper;
    private SQLiteDatabase database;
    private Post post;
    private ArrayList posts;
    private ScrollView scrollView;
    private ListView listView;
    private SimpleCursorAdapter dataAdapter;

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

        databaseHelper = new DatabaseHelper(this);
        database = databaseHelper.getWritableDatabase();
    }


}