Android 设置/获取SQLite值时出现问题
我只是在Android中设置SQLite表的基础知识,但在存储/检索任何值方面都失败了。我无法判断我是在存储方面还是在检索方面失败,只是我无法返回任何值。以下是我正在做的: 现在我只有一个表,其中有两列(实际上有三列,包括id字段)。第1列是文件名,第2列是我要存储的md5哈希值。因此,如果从应用程序中的某个地方,我称之为:Android 设置/获取SQLite值时出现问题,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我只是在Android中设置SQLite表的基础知识,但在存储/检索任何值方面都失败了。我无法判断我是在存储方面还是在检索方面失败,只是我无法返回任何值。以下是我正在做的: 现在我只有一个表,其中有两列(实际上有三列,包括id字段)。第1列是文件名,第2列是我要存储的md5哈希值。因此,如果从应用程序中的某个地方,我称之为: String fileName = "image.png" String md5 = "c32005e7c4929527ff67727a24430bc0" MyDB db
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);
}
}
}
函数
getSDCardFileMD5Hash
未正确检查现有记录;如果存在多个匹配记录,则条件getCount()==1
将失败。
您应该只使用if(cursor.moveToFirst())
要首先防止将多个相同的记录插入数据库,请确保要检查的列上有一个
唯一的约束,并使用(…,CONFLICT\u IGNORE)
getCount()代替插入
==1
当您多次运行应用程序时,将失败。您是对的。它应该是setter函数中的db.replace。如果你把它作为一个答案,我会接受的。谢谢