我尝试运行“ getItem()”方法
getItem()
公共类PhoneDal扩展了SQLiteOpenHelper {
// Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = Constants.DB_NAME; private static final String BLOCKED_PHONES_TABLE = "BLOCKED_PHONES_TABLE"; private static final String COMMENTS_TABLE = "COMMENTS_TABLE"; public PhoneDal(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // SQL statement to create book table String CREATE_BLOCKED_PHONES_TABLE = "CREATE TABLE " + BLOCKED_PHONES_TABLE + " ( " + KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_PHONE+" TEXT, " + KEY_IS_BLOCKED+" BIT )"; db.execSQL(CREATE_BLOCKED_PHONES_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if (newVersion > oldVersion) { Log.w("MyAppTag", "Updating database from version " + oldVersion + " to " + newVersion + " .Existing data will be lost."); // Drop older books table if existed db.execSQL("DROP TABLE IF EXISTS " + BLOCKED_PHONES_TABLE); // create fresh books table this.onCreate(db); } } private static final String KEY_ID = "id"; private static final String KEY_PHONE = "KEY_PHONE"; private static final String KEY_IS_BLOCKED = "KEY_IS_BLOCKED"; public Phone getItem(String phone) { Phone result = new Phone(); Cursor cursor = this.getReadableDatabase().query(BLOCKED_PHONES_TABLE, new String[] { KEY_ID }, ""+KEY_PHONE+" = ? AND "+KEY_IS_BLOCKED+" = ?", new String[] { phone, "1" }, null, null, null); // 3. if we got results get the first one if (cursor != null) { result.id = (cursor.getInt(1)); result.phone = phone; result.isBlocked = false; } return result; }
我得到这个异常:
Process: com.example.stopcall.app, PID: 31146 android.database.sqlite.SQLiteException: no such column: KEY_PHONE (code 1): , while compiling: SELECT id FROM BLOCKED_PHONES_TABLE WHERE KEY_PHONE = ? AND KEY_IS_BLOCKED = ? at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1448) at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1295) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1166) at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1334) at com.example.stopcall.app.dal.PhoneDal.getItem(PhoneDal.java:117) at com.example.stopcall.app.ItemDetailFragment$1.onClick(ItemDetailFragment.java:93) at android.view.View.performClick(View.java:4654) at android.view.View$PerformClick.run(View.java:19438) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method)
为什么我看到:
public void setWriteAheadLoggingEnabled(boolean enabled) { throw new RuntimeException("Stub!"); } public SQLiteDatabase getWritableDatabase() { throw new RuntimeException("Stub!"); } public SQLiteDatabase getReadableDatabase() { throw new RuntimeException("Stub!"); }
每次代码都到达这里
您正在查看android.jar文件的反编译版本,而不是在设备或仿真器上运行的实际Android代码。
android.jar
关于实际问题,我的猜测是您在不更改架构版本(DATABASE_VERSION在代码中)的情况下更改了数据库架构。增量DATABASE_VERSION并实施onUpdate(),或者卸载并重新安装您的应用以摆脱旧数据库。
DATABASE_VERSION
onUpdate()