mercredi 20 mai 2015

Cannot find the SQLite db file on my device

I created a database in the following code.

public final class MembershipDatabaseHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "members.db";
    private static final String DB_TABLE = "members";
    private static final int DB_VERSION = 1;
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_FIRSTNAME = "firstname";
    private static final String COLUMN_LASTNAME = "lastname";
    private static final String COLUMN_PASSWORD = "password";
    private static final String COLUMN_DOB = "dob";
    private static final String CREATE_TABLE = "CREATE TABLE " + DB_TABLE + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COLUMN_EMAIL + " TEXT NOT NULL, " + COLUMN_FIRSTNAME + " TEXT NOT NULL, " + COLUMN_LASTNAME + " TEXT NOT NULL, "
        + COLUMN_PASSWORD + " TEXT NOT NULL, " + COLUMN_DOB + " TEXT NOT NULL);";

    private static String queryDrop = "DROP TABLE IF EXISTS " + DB_TABLE;

    SQLiteDatabase db;

    public MembershipDatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
        this.db = db;
    }

    public void signup(User user) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        String query = "SELECT * FROM " + DB_TABLE;
        Cursor cursor = db.rawQuery(query, null);
        int count = cursor.getCount();

        values.put(COLUMN_ID, count);
        values.put(COLUMN_EMAIL, user.getEmail());
        values.put(COLUMN_FIRSTNAME, user.getFirstname());
        values.put(COLUMN_LASTNAME, user.getLastname());
        values.put(COLUMN_PASSWORD, user.getPassword());
        values.put(COLUMN_DOB, user.getDob());
        db.insert(DB_TABLE, null, values);
        db.close();
    }

    public String searchPassword(String email) {
        db = this.getReadableDatabase();
        String query = "SELECT " + COLUMN_EMAIL + ", " + COLUMN_PASSWORD + " FROM " + DB_TABLE;
        Cursor cursor = db.rawQuery(query, null);

        String targetEmail;
        String password = "NOT FOUND";

        if(cursor.moveToFirst()) {
            do {
                targetEmail = cursor.getString(0);

                if(targetEmail.equals(email)) {
                    password = cursor.getString(1);
                    break;
                }
            } while(cursor.moveToNext());
        }
        return password;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(queryDrop);
        this.onCreate(db);
    }
}

On many blogs, it says that without specifically designating the path, the database file (members.db in this code) is stored in /data/data/(package name)/databases/(databasename).db. But after creating the apk file and installing the app and running, I still cannot find the db file anywhere (Even cannot find the directory under the /data/ directory itself either).

Some recommend using DDMS on Eclipse, but I cannot find the file either. Am I getting something wrong with the concepts or are there any errors with my code?

Aucun commentaire:

Enregistrer un commentaire