0

我只是在 Android 中设置 SQLite 表的基础知识,但我无法存储/检索任何值。我不知道我是在存储方面还是在检索方面失败了——只是我无法返回任何值。这是我正在做的事情:

现在我只有一个包含两列的表(实际上是三列,包括 id 字段)。第 1 列是文件名,第 2 列是我要存储的 md5 哈希值。所以,如果从应用程序的某个地方,我称之为:

String fileName = "image.png"
String md5 = "c32005e7c4929527ff67727a24430bc0"
MyDB db = new MyDB(getApplicationContext());
System.err.println("setting md5 for " + fileName + ": " + md5);
int returnValue = db.setSDCardFileMetadata(fileName, md5);
db.closeDatabase();

然后我应该看到System.err.println输出,因为值是在相应的 SQLite 数据库方法中设置和检索的,如下所示:

setting md5 for image.png: c32005e7c4929527ff67727a24430bc0
inserting row for: image.png: c32005e7c4929527ff67727a24430bc0
Verifying image.png: c32005e7c4929527ff67727a24430bc0 ###

但是,相反,我看到的是:

setting md5 for image.png: c32005e7c4929527ff67727a24430bc0
inserting row for: image.png: c32005e7c4929527ff67727a24430bc0
no matching file found on sd card: image.png
Verifying image.png:  ###

看看 getter 函数如何找不到匹配的值?我不知所措。在我看来一切都很好。谁能看到我的错误?谢谢!

public class MyDB {

    /* The index (key) column name for use in where clauses. */
    public static final String KEY_ID = "_id";

    /* Tables and db information */
    private static final String DB_NAME = "MyDB.db";
    private static final int DB_VERSION = 1;
    private static final String SDCARD_FILES_TABLE = "SDCARD_FILES_TABLE";

    /* The name and column index of each column in the database */
    private static final String FILE_NAME_COLUMN =  "FILE_NAME_COLUMN";
    private static final String MD5_COLUMN = "MD5_COLUMN";

    /* Database open/upgrade helper */
    private static MyDBOpenHelper myDBOpenHelper;

    public MyDB(Context context) {
        myDBOpenHelper = new MyDBOpenHelper(context, DB_NAME, null, DB_VERSION);    
    }

    public static SQLiteDatabase getDB() {
        SQLiteDatabase db = myDBOpenHelper.getWritableDatabase();
        return db;
    }

    /* Called when we no longer need access to the database. */
    public void closeDatabase() {
        myDBOpenHelper.close();
    }

    public int setSDCardFileMetadata(String file, String md5hash) {
        SQLiteDatabase db = myDBOpenHelper.getWritableDatabase();
        ContentValues newValues = new ContentValues();

        newValues.put(FILE_NAME_COLUMN, file);
        newValues.put(MD5_COLUMN, md5hash);
System.err.println("inserting row for: " + file);
        int returnValue = (int) db.insert(SDCARD_FILES_TABLE, null, newValues);

        System.err.println("Verifying " + file + ": " + getSDCardFileMD5Hash(file) + " ###");

        return returnValue;

    }

    public String getSDCardFileMD5Hash(String file) {
        SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
        String where = FILE_NAME_COLUMN + "= ?";
        Cursor cursor = db.query(SDCARD_FILES_TABLE, new String[] {MD5_COLUMN}, where, new String[] {file}, null, null, null);
        if (cursor.getCount() == 1) {
            cursor.moveToFirst();
            return cursor.getString(0);
        }
        System.err.println("no matching file found on sd card: " + file);
        return "";
    }


    private static class MyDBOpenHelper extends SQLiteOpenHelper {

        public MyDBOpenHelper(Context context, String name, CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        /* SQL Statement to create a metadata table for files stored on the sd card */
        private static final String SDCARD_FILES_TABLE_CREATE = "create table " +
            SDCARD_FILES_TABLE + " (" + KEY_ID +
            " integer primary key autoincrement, " +
            FILE_NAME_COLUMN + " text not null, " +
            MD5_COLUMN + " text not null);";

        /* Create the new database */
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SDCARD_FILES_TABLE_CREATE);
            System.err.println(SDCARD_FILES_TABLE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,
            int newVersion) {
            db.execSQL("DROP TABLE IF IT EXISTS " + SDCARD_FILES_TABLE);
        onCreate(db);
        }
    }

}
4

1 回答 1

0

该函数getSDCardFileMD5Hash未正确检查现有记录;getCount() == 1如果有多个匹配记录,则条件将失败。你应该只使用if (cursor.moveToFirst()).

要首先防止将多个相同的记录插入数据库,请确保UNIQUE对要检查的列有约束,而不是insert使用.insertWithOnConflict(..., CONFLICT_IGNORE)

于 2013-08-26T07:55:22.483 回答