mardi 23 juin 2015

Can't detect SQLite records

When I try to search and display the data after I input them it will simply not show anything for some reason. Just a blank page as if there was nothing. Please check my code and see if there is something wrong and ask me for anything necessary.

public class PInfoDatabase extends SQLiteOpenHelper {
    private static final String TAG = PInfoDatabase.class.getSimpleName();
    private static final String DATABASE_NAME = "pinfos.db";
    private static final int DATABASE_VERSION = 2;
    private final Context mContext;

interface Tables{
    String PINFOS = "pinfos";

}

public PInfoDatabase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    mContext=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + Tables.PINFOS + " ("
            + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + PInfoContract.PInfoColumns.PINFO_WEBSITE  + " TEXT NOT NULL,"
            + PInfoContract.PInfoColumns.PINFO_USERNAME + " TEXT NOT NULL,"
            + PInfoContract.PInfoColumns.PINFO_PASSWORD + " TEXT NOT NULL,"
            + PInfoContract.PInfoColumns.PINFO_EMAIL    + " TEXT NOT NULL)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    int version = oldVersion;
    if (version == 1) {
        //add some extra fields to the database without deleting existing data
        version = 2;
    }

    if (version != DATABASE_VERSION) {
        db.execSQL("DROP TABLE IF EXISTS " + Tables.PINFOS);
        // Do that for all your tables.
        onCreate(db);
        /*
        What is done will delete all the data when upgrading.
        When needed to add rows/columns use:
        //this is the method I designed to solve the problem. I'm not sure if it's redundant in parts but it works great.
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         //create temp. table to hold data
                     db.execSQL("CREATE TEMPORARY TABLE contacts_backup (_id INTEGER PRIMARY KEY AUTOINCREMENT, column_1 TEXT, column_2 TEXT);");
        //insert data from old table into temp table

        db.execSQL("INSERT INTO contacts_backup SELECT _id, column_1, column_2 FROM contacts ");
        //drop the old table now that your data is safe in the temporary one
        db.execSQL("DROP TABLE contacts");
        //recreate the table this time with all 4 columns
        db.execSQL("CREATE TABLE contacts (_id INTEGER PRIMARY KEY AUTOINCREMENT, column_1 TEXT, column_2 TEXT, column_3 TEXT);");
        //fill it up using null for the column_3
        db.execSQL("INSERT INTO contacts SELECT _id, column_1, column_2, null FROM contacts_backup");
        //then drop the temporary table
        db.execSQL("DROP TABLE contacts_backup");
       */
    }
}

    public static void deleteDatabase(Context context) {
        context.deleteDatabase(DATABASE_NAME);
    }


}

The class with the insert delete, update methods:

public class PInfoProvider extends ContentProvider {
private PInfoDatabase mOpenHelper;

private static String TAG = PInfoProvider.class.getSimpleName();
private static final UriMatcher sUriMatcher = buildUriMatcher();

private static final int PINFOS = 100;
private static final int PINFOS_ID = 101;

private static UriMatcher buildUriMatcher(){
    final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    final String authority = PInfoContract.CONTENT_AUTHORITY;
    matcher.addURI(authority, "PInfos", PINFOS);
    matcher.addURI(authority, "Pinfos/*", PINFOS_ID);
    return matcher;
}
@Override
public boolean onCreate() {
    mOpenHelper = new PInfoDatabase(getContext());
    return true;
}

private void deleteDatabase(){
    mOpenHelper.close();
    PInfoDatabase.deleteDatabase(getContext());
    mOpenHelper = new PInfoDatabase(getContext());
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    final SQLiteDatabase db = mOpenHelper.getReadableDatabase();
    final int match = sUriMatcher.match(uri);

    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(PInfoDatabase.Tables.PINFOS);

    switch(match){
        case PINFOS:
            //do nothing
            break;
        case PINFOS_ID:
            String id = PInfoContract.PInfos.getPInfoId(uri);
            queryBuilder.appendWhere(BaseColumns._ID + "=" + id);
            break;
        default:
            throw new IllegalArgumentException("Unknown Uri: " + uri);
    }

    Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    //projection in content provider means the list of columns you want to return.
    return cursor;

}

@Override
public String getType(Uri uri) {
    final int match = sUriMatcher.match(uri);
    switch(match){
        case PINFOS:
            return PInfoContract.PInfos.CONTENT_TYPE;
        case PINFOS_ID:
            return PInfoContract.PInfos.CONTENT_ITEM_TYPE;
        default:
            throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    //ContentValues: is a list of content values of our database such as the email and username. Contains the column names and the values we want to associate to it when we're writing to the database/
    Log.v(TAG, "insert(uri=" + uri + ", values =" + values.toString());

    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    final int match = sUriMatcher.match(uri);

    switch(match){
        case PINFOS:             //only using the PInfo and not the ID becuase it's to insert only.
            long recordID = db.insertOrThrow(PInfoDatabase.Tables.PINFOS, null, values); //inserts a row into the database
            return PInfoContract.PInfos.buildPInfoUri(String.valueOf(recordID));

        default:
            throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    Log.v(TAG, "delete(uri=" + uri);

    if(uri.equals(PInfoContract.URI_TABLE)){
        deleteDatabase();
        return 0;
        //This will be executed if the user didn't input a valid record id.
        //Base content uri doesn't contain an ID nor a path.
    }
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    final int match = sUriMatcher.match(uri);

    String selectionCriteria = selection;

    switch(match){

        /*
        case PInfo:
            If this was called and didn't "break" it would change all the records in the table.
            We will still leave it out because we added the database deletion code above.
             break;
         */

        case PINFOS_ID:
            String id = PInfoContract.PInfos.getPInfoId(uri);
            selectionCriteria = BaseColumns._ID + "=" + id
                    + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
            return db.delete(PInfoDatabase.Tables.PINFOS, selectionCriteria, selectionArgs);

        default:
            throw new IllegalArgumentException("Unknown Uri: " + uri);
    }

}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    Log.v(TAG, "update(uri=" + uri + ", values =" + values.toString());

    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    final int match = sUriMatcher.match(uri);

    String selectionCriteria = selection;
    //We set selectionCriteria to equal selection in the case PInfo case was chosen. After all,
    //we still need to record the selection.

    switch(match){
        case PINFOS:
            //do nothing
            //If this was called and didn't "break" it would change all the records in the table.
            break;
        case PINFOS_ID:
            String id = PInfoContract.PInfos.getPInfoId(uri);
            selectionCriteria = BaseColumns._ID + "=" + id
                + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
            break;

        default:
            throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
    return db.update(PInfoDatabase.Tables.PINFOS, values, selectionCriteria, selectionArgs);
}

}

Aucun commentaire:

Enregistrer un commentaire