1

无法插入使用 SQLite 数据库的内容提供程序。

只是试图实现一个简单的键值表,它有两列,键和值,它们都是TEXT.

相关代码和堆栈跟踪。

// SQL for creating database
private static final String DATABASE_CREATE =
        "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + KEY +
        " TEXT, " + VALUE + " TEXT);";

// The insert method
@Override
public Uri insert(Uri uri, ContentValues values) {
    SQLiteDatabase mdb = mdbHelper.getWritableDatabase();

    long key = mdb.insert(MessagesDbHelper.TABLE_NAME, null, values);
    mdb.close();

    return Uri.parse(URI + "/" + key);
}

// The insert test
private boolean testInsert() {
    try {
        for (int i = 0; i < TEST_CNT; i++) {
            mContentResolver.insert(mUri, mContentValues[i]);
        }
    } catch (Exception e) {
        return false;
    }

    return true;
}

// initializes content values for insert test
private ContentValues[] initTestValues() {
    ContentValues[] cv = new ContentValues[TEST_CNT];
    for (int i = 0; i < TEST_CNT; i++) {
        cv[i] = new ContentValues();
        cv[i].put(KEY_FIELD, "key" + Integer.toString(i));
        cv[i].put(VALUE_FIELD, "val" + Integer.toString(i));
    }

    return cv;
}

// static variables
private static final String KEY_FIELD = "key";
private static final String VALUE_FIELD = "value";

追踪开始:

03-04 02:09:18.553: E/SQLiteLog(1531): (20) statement aborts at 6: [INSERT INTO messages(value,key) VALUES (?,?)] datatype mismatch
03-04 02:09:18.573: E/SQLiteDatabase(1531): Error inserting value=val49 key=key49
03-04 02:09:18.573: E/SQLiteDatabase(1531): android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
4

0 回答 0