Question

I am having trouble querying a SQLite database on my Android app. I used this tutorial to set up my database helper class.

I made a simple query function:

public Cursor queryDB(String Query,String[] args){  

Cursor cursor = myDataBase.rawQuery(Query, args);

return cursor;

and then I call it like this:

 String[] testarg = {"Denver Bake"};
 myDataBase.getReadableDatabase();
 Cursor results = myDataBase.queryDB("SELECT * FROM Recipes WHERE r_name = ?", testarg);

The error I am getting is:

 E/AndroidRuntime(30483): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dbtest/com.example.dbtest.MainActivity}: android.database.sqlite.SQLiteException: no such table: Recipes: , while compiling: SELECT * FROM Recipes WHERE r_name = ?

I know that the Recipes table does exist in my database because it appears in the SQLite database browser in the tutorial. I've tried some of the suggestions in the tutorial comments but I still get the same error, and I'm not really sure what to do now.

Thanks is advance.

Here is my create

public void createDataBase() throws IOException{
    boolean dbExist = checkDataBase();
    if(dbExist){
            //do nothing - database already exist
    }else{

        this.getReadableDatabase();
        try {
            copyDataBase();
            } catch (IOException e) {

            throw new Error("Error copying database");
            }
    }
}

And here is my copy code:

private void copyDataBase() throws IOException{
    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);
    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
       }
    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

Lastly, I do have my database in my assets folder. The error message is telling me that the table in the database does not exist.

Was it helpful?

Solution 2

CL was pretty much right. My copy code was right it wasn't being called correctly. What I ended up doing was sloppy but I called copyDataBase() from my query code rather then my create database. Stupid and inefficient I know, but it works now.

OTHER TIPS

You must create your own SQLite database in the browser and copy this databse into the assets folder of your project:

  1. create database in browser.
  2. copy database
  3. paste in your assets folder.
  4. create some dbhelper class that extends the SQLiteOpenHelper class and copies the database out of the assets folder so that the app can access it:
public class DbHelper extends SQLiteOpenHelper {

    // The Android's default system path of your application database.
    private static String PACKAGENAME = "com.SVLL";
    private static String DB_PATH = "/data/data/" + PACKAGENAME + "/databases/";
    private static String DB_NAME = "SVLL.sqlite";

    private static final String TAG = "DbHelper";
    private SQLiteDatabase myDataBase;

    private final Context myContext;

    /**
     * Constructor Takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     * 
     * @param context
     */
    public DbHelper(final Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;

    }

    /**
     * Creates a empty database on the system and rewrites it with your own
     * database.
     * */
    public final void createDataBase() throws IOException {

        final boolean dbExist = checkDataBase();
        SQLiteDatabase db_Read = null;
        if (dbExist) {
            // do nothing - database already exist
        } else {
            // By calling this method and empty database will be created into
            // the default system path
            // of your application so we are gonna be able to overwrite that
            // database with our database.
            // By calling this method and empty database will be created into
            // the default system path
            // of your application so we are gonna be able to overwrite that
            // database with our database.
            // db_Read = this.getReadableDatabase(DB_Internal);
            db_Read = this.getReadableDatabase();
            db_Read.close();

            copyDataBase();

        }
    }

    /**
     * Restore whole database without any data
     * 
     * @throws IOException
     */
    public final void RestoreDatabase() throws IOException {
        SQLiteDatabase db_Read = this.getReadableDatabase();
        db_Read.close();

        copyDataBase();
        Log.i(TAG, "Database REstored");
    }

    /**
     * Check if the database already exist to avoid re-copying the file each
     * time you open the application.
     * 
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase() {
        final File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();
    }

    /**
     * Copies your database from your local assets-folder to the just created
     * empty database in the system folder, from where it can be accessed and
     * handled. This is done by transfering bytestream.
     * 
     * @throws IOException
     * */
    private void copyDataBase() throws IOException {

        // Open your local db as the input stream
        final InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        final String outFileName = DB_PATH + DB_NAME;

        // Open the empty db as the output stream
        final OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile
        final byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public final SQLiteDatabase openDataBase() {
        // Open the database
        final String myPath = DB_PATH + DB_NAME;
        if (myDataBase != null && myDataBase.isOpen()) {
            myDataBase.close();
        }
        return myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);

    }

    @Override
    public final synchronized void close() {
        if (myDataBase != null)
            myDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(final SQLiteDatabase arg0) {
    }

    @Override
    public void onUpgrade(final SQLiteDatabase arg0, final int arg1,
            final int arg2) {
    }

}

Try this:

public class DBAdapter {

private static final String TAG = "[ DBAdapter ]";  
private static final String KEY_TOU_ID = "tournamentid";    
private static final String KEY_TEAM_ID = "teamid"; 
private static final String KEY_INNINGS_ID = "inningsid";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "YourDbName";   
private static final String INNINGS_LIST_TABLE = "inningslist";



private static final String INNINGS_TABLE = "create table "
        + INNINGS_LIST_TABLE + "(" + KEY_INNINGS_ID
        + " integer primary key autoincrement," + KEY_GAME_ID + " integer,"
        + KEY_TEAM_ID + " integer," + KEY_TOU_ID + " integer);";

private DatabaseHelper DBhelper;
private SQLiteDatabase db;
private ProgressDialog pd;

public DBAdapter(Context ctx) {
    DBhelper = new DatabaseHelper(ctx);
}

private static class DatabaseHelper extends SQLiteOpenHelper {

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

    @Override
    public void onCreate(SQLiteDatabase db) {



        Log.i(TAG, "INNINGS TABLE" + INNINGS_LIST_TABLE);

        db.execSQL(INNINGS_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
    }
}

public DBAdapter open() {
    try {
        db = DBhelper.getWritableDatabase();
    } catch (SQLiteException sql) {
        sql.printStackTrace();
    }
    return this;
}

public boolean isCreated() {
    if (db != null) {
        return db.isOpen();
    }

    return false;
}

public boolean isOpen() {
    return db.isOpen();
}

public void close() {
    DBhelper.close();
    db.close();
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top