mardi 1 décembre 2015

android.database.sqlite.SQLiteException: no such column: items

I know this will be a simple solution. surely there's something I don't see in my code but at this point, I've been looking at my code for hours. I need a new set of eyes! I've uninstalled the apps but I still get this error

Caused by: android.database.sqlite.SQLiteException: no such column: items (code 1): , while compiling: SELECT id, title, quantity, unit, expiry_date, note FROM items WHERE (id = items)

Anyway these are my codes

Constants.java

public class Constants {

    public static final String ITEMS_TABLE = "items";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_QUANTITY = "quantity";
    public static final String COLUMN_UNIT = "unit";
    public static final String COLUMN_TITLE = "title";
    public static final String COLUMN_EXPIRY_DATE = "expiry_date";
    public static final String COLUMN_NOTE = "note";

public static final String[] COLUMNS = {
    Constants.COLUMN_ID,
    Constants.COLUMN_TITLE,
    Constants.COLUMN_QUANTITY,
    Constants.COLUMN_UNIT,
    Constants.COLUMN_EXPIRY_DATE,
    Constants.COLUMN_NOTE
};
}

Content Provider

private DbHelper dbHelper;
private static final String BASE_PATH_ITEM = "items";
private static final String AUTHORITY = "packagename.XContentProvider";
public static final Uri ITEM_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH_ITEM);
private static final int ITEM = 100;
private static final int ITEMS = 101;

private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
static {
    URI_MATCHER.addURI(AUTHORITY, BASE_PATH_ITEM, ITEMS);
    URI_MATCHER.addURI(AUTHORITY, BASE_PATH_ITEM + "/#", ITEM);

}

private void checkColumns(String[] projection) {
    if (projection != null) {
        HashSet<String> request = new HashSet<String>(Arrays.asList(projection));
        HashSet<String> available = new HashSet<String>(Arrays.asList(Constants.COLUMNS));
        if (!available.containsAll(request)) {
            throw new IllegalArgumentException("Unknown columns in projection");
        }
    }
}

@Override
public boolean onCreate() {
    dbHelper = new DbHelper(getContext());
    return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(Constants.ITEMS_TABLE);
    checkColumns(projection);

    int type = URI_MATCHER.match(uri);
    switch (type){
        case ITEM:
            //there is not to do if the query is for the table
            break;
        case ITEMS:
            queryBuilder.appendWhere(Constants.COLUMN_ID + " = " + uri.getLastPathSegment());
            break;
        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}

@Override
public String getType(Uri uri) {
    return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    int type = URI_MATCHER.match(uri);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    Long id;
    switch (type){
        case ITEMS:
            id = db.insert(Constants.ITEMS_TABLE, null, values);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return Uri.parse(BASE_PATH_ITEM + "/" + id);
}


@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    int type = URI_MATCHER.match(uri);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    int affectedRows;
    switch (type) {
        case ITEMS:
            affectedRows = db.delete(Constants.ITEMS_TABLE, selection, selectionArgs);
            break;
        case ITEM:
            String id = uri.getLastPathSegment();
            if (TextUtils.isEmpty(selection)) {
                affectedRows = db.delete(Constants.ITEMS_TABLE, Constants.COLUMN_ID + "=" + id, null);
            } else {
                affectedRows = db.delete(Constants.ITEMS_TABLE, Constants.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
            }
            break;

        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return affectedRows;
}


@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    int type = URI_MATCHER.match(uri);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    int affectedRows;
    switch (type) {
        case ITEMS:
            affectedRows = db.update(Constants.ITEMS_TABLE, values, selection, selectionArgs);
            break;

        case ITEM:
            String id = uri.getLastPathSegment();
            if (TextUtils.isEmpty(selection)) {
                affectedRows = db.update(Constants.ITEMS_TABLE, values, Constants.COLUMN_ID + "=" + id, null);
            } else {
                affectedRows = db.update(Constants.ITEMS_TABLE, values, Constants.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
            }
            break;

        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return affectedRows;
}
}

database

public class DbHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "myDB.db";
private static final int DATABASE_VERSION = 1;



public DbHelper(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {

    //create the required table
    db.execSQL(CREATE_TABLE_ITEM);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + Constants.ITEMS_TABLE);
    //create new table
    onCreate(db);
}

//create pantry table statement
private static final String CREATE_TABLE_ITEM = "create table "
        + Constants.ITEMS_TABLE
        + "("
        + Constants.COLUMN_ID + " integer primary key autoincrement, "
        + Constants.COLUMN_TITLE + " text not null, "
        + Constants.COLUMN_QUANTITY + " integer not null, "
        + Constants.COLUMN_UNIT + " text not null, "
        + Constants.COLUMN_EXPIRY_DATE + " text not null, "
        + Constants.COLUMN_NOTE + " text not null "
        + ")";
}

Thanks.

Aucun commentaire:

Enregistrer un commentaire