dimanche 11 octobre 2015

No such table: table_image (code 1): , while compiling: SELECT image_data FROM table_image WHERE image_name= ' a '

I am a bit new to SQLite so please bear with me. I am creating a table and trying to access data from it but somehow I am getting this error.

android.database.sqlite.SQLiteException: no such table: table_image (code 1): , while compiling: SELECT image_data FROM table_image WHERE image_name= ' a '

Things that I did after the error came:

1.) Uninstalled the app and installed it again. 2.) Checked for spaces in the table creation code. It looks right to me.

I am not sure why this error is appearing then. Can someone please help me. Thanks !!

My DatabaseHelper class

public class DatabaseHelper extends SQLiteOpenHelper {
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "database_name";

    // Table Names
    public static final String DB_TABLE = "table_image";

    // column names
    public static final String KEY_NAME = "image_name";
    public static final String KEY_IMAGE = "image_data";

    // Table create statement
    private static final String CREATE_TABLE_IMAGE = "CREATE TABLE " + DB_TABLE + " ("+
            KEY_NAME + " TEXT, " +
            KEY_IMAGE + " BLOB"+")";

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

    @Override
    public void onCreate(SQLiteDatabase db) {

        // creating table
        db.execSQL(CREATE_TABLE_IMAGE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // on upgrade drop older tables
        db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);

        // create new table
        onCreate(db);
    }
}

Adding stuff to db in another class

// Calling addEntry

addEntry(name, img);

// Function to add entry

public void addEntry( String name, byte[] image) throws SQLiteException {
        ContentValues cv = new ContentValues();
        cv.put(DatabaseHelper.KEY_NAME,    name);
        cv.put(DatabaseHelper.KEY_IMAGE,   image);
        database.insert(DatabaseHelper.DB_TABLE, null, cv);
    }

Then I am tying to get data from it.

SQLiteDatabase db = openOrCreateDatabase(DatabaseHelper.DB_TABLE, MODE_PRIVATE, null);
String selectQuery = "SELECT image_data FROM "+DatabaseHelper.DB_TABLE+" WHERE image_name= ' "+"a"+" ' ";
Cursor cursor = db.rawQuery(selectQuery,null);
byte[] image = cursor.getBlob(1);

Aucun commentaire:

Enregistrer un commentaire