簡體   English   中英

為什么我的sqlite選擇沒有返回結果?

[英]Why does my sqlite selection return no results?

我已經在Android App Db中添加了一些記錄。

然后,我嘗試檢索這些行,但始終將光標設置在位置-1。

我的語法有什么問題?

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;

    public static final String BLOCKED_PHONES_TABLE = "BLOCKED_PHONES_TABLE";

    public PhoneDal(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_BLOCKED_PHONES_TABLE =
                "CREATE TABLE "+ BLOCKED_PHONES_TABLE +
                        " ( "+ KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL  DEFAULT 1, "
                        + 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 long addItem(Phone phone) {
        Log.d(Constants.LOGGER_TAG, "add saved-offer");
        // 1. get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();

        // 2. create ContentValues to add key "column"/value
        ContentValues values = new ContentValues();
        //values.put(KEY_ID, phone.id);
        values.put(KEY_PHONE, phone.phone);
        values.put(KEY_IS_BLOCKED, phone.isBlocked);

        // 3. insert
        long newRowId =
                db.insertWithOnConflict(BLOCKED_PHONES_TABLE, KEY_ID,
                values, SQLiteDatabase.CONFLICT_REPLACE);

        if (newRowId > 0) {
            final String text = String.format("item was added to table: %s",
                    BLOCKED_PHONES_TABLE);
            Log.d(Constants.LOGGER_TAG, text);

        }

        // 4. close
        db.close();
        return newRowId;
    }

    public Phone getItem(String phone) {

        Phone result = null;

        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 && cursor.getPosition() != -1) {
            result = new Phone();
            result.id = (cursor.getInt(1));
            result.phone = phone;
            result.isBlocked = true;
        }
        return result;
    }
}

在此處輸入圖片說明

我試圖創建一個select *查詢,但沒有結果(光標在位置-1)

            BLOCKED_PHONES_TABLE,
            new String[] { KEY_ID  },null, null, null, null, null);

在此處輸入圖片說明

這是一個功能。 查詢和接收游標時,它始終始終首先指向索引-1。 在嘗試訪問數據之前,必須先在其上調用moveTo...()方法之一。 這些方法返回一個布爾值,該值告訴您在移動后光標是否指向有效行。

例如,更改此:

// 3. if we got results get the first one
if (cursor != null && cursor.getPosition() != -1) {

// 3. if we got results get the first one
if (cursor.moveToFirst()) {

SQLiteDatabase查詢方法從不返回null,因此檢查其中的一種是多余的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM