android菜鸟

public Person find(Integer id) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from person where personId=?",
new String[] { id.toString()});
if (cursor.moveToFirst()) {
int personid = cursor.getInt(cursor.getColumnIndex(" personId"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
String phone = cursor.getString(cursor.getColumnIndex("phone"));
return new Person(personid, name, age, phone);
}
cursor.close();
return null;
}

原来SQLite 数据库是有区分字母大小写的,修改前,运行提示出错:
java.lang.IllegalStateException: get field slot from row 0 col -1 failed
at android.database.CursorWindow.getLong_native(Native Method)
at android.database.CursorWindow.getInt(CursorWindow.java:434)
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:93)
at cn.lyun.service.PersonService.find(PersonService.java:60)
at cn.lyun.test.PersonServiceTest.testFind(PersonServiceTest.java:32)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

你可能感兴趣的:(android,易错)