Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

java - SQLiteDatabase getWritableDatabase() { throw new RuntimeException("Stub!"); }

I try to run "getItem()" method

public class PhoneDal extends 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;
}

I get this exception:

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)

why do I see that:

public void setWriteAheadLoggingEnabled(boolean enabled) {
    throw new RuntimeException("Stub!");
}

public SQLiteDatabase getWritableDatabase() {
    throw new RuntimeException("Stub!");
}

public SQLiteDatabase getReadableDatabase() {
    throw new RuntimeException("Stub!");
}

the code gets here every time

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are looking at a decompiled version of the android.jar file, not the actual Android code that runs on a device or emulator.

With respect to the actual problem, my guess is that you changed your database schema without changing the schema version (DATABASE_VERSION in your code). Either increment DATABASE_VERSION and implement onUpdate(), or uninstall and reinstall your app to get rid of your old database.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...