簡體   English   中英

Android sqlite游標查詢

[英]Android sqlite cursor query

我正在關注http://www.vogella.de/articles/AndroidSQLite/article.html教程。 我現在正在嘗試更改系統,因此不僅存在字段“ comment”,而且存在字段“ item”和“ price”。

現在,我有2個Coloums而不是一個,光標似乎正在導致應用程序崩潰! 例如,下面是下面的代碼:

        public List<Item> getAllItems() {
            List<Item> items = new ArrayList<Item>();
            Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
                    allColumns, null, null, null, null, null);
            cursor.moveToFirst();

            while (!cursor.isAfterLast()) {
                Item item = cursorToItem(cursor);
                items.add(item);
                cursor.moveToNext();
            }
            // Make sure to close the cursor
            cursor.close();
            return items;
        }

        private Item cursorToItem(Cursor cursor) {
            Item item = new Item();
            item.setId(cursor.getLong(0));
            item.setItem(cursor.getString(1));
            item.setPrice(cursor.getString(2));
            return item;
        }

LINE:

 Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
                    allColumns, null, null, null, null, null);

是導致錯誤的原因,我認為這與我有另外一列有關。 有人可以告訴我如何編輯此行以使其正常工作嗎,我已經嘗試過但不理解此游標查詢。

編輯:通過將所有列替換為null來運行。 但是現在它崩潰了:

public Item createItem(String item, String price) {
        ContentValues values = new ContentValues();
        values.put(MySQLiteHelper.COLUMN_ITEM, item);
        values.put(MySQLiteHelper.COLUMN_PRICE, price);
        long insertId = database.insert(MySQLiteHelper.TABLE_ITEMS, null,
                values);
        // To show how to query
        Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
                allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        return cursorToItem(cursor);
    }

在線:

Cursor cursor = database.query(MySQLiteHelper.TABLE_ITEMS,
                allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
                null, null, null);

這是MySQLHelper.js:

package nupos.nupay.app;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {

public static final String TABLE_ITEMS = "items";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_ITEM = "item";
public static final String COLUMN_PRICE = "price";

private static final String DATABASE_NAME = "items_test.db";
private static final int DATABASE_VERSION = 1;

// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
        + TABLE_ITEMS + "( " + COLUMN_ID
        + " integer primary key autoincrement, " + COLUMN_ITEM
        + " text not null," + COLUMN_PRICE
        +"  text not null);";

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

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEMS);


    onCreate(db);
}

 }

編輯:問題是最初創建數據庫時沒有其中一個字段。

恕我直言, allColumns不包含MySQLiteHelper.TABLE_ITEMS定義。 將所有參與對象的聲明和當前值放在此處。

另一種可能性- database對象未正確初始化,目前為空。 您應該檢查這種情況。

暫無
暫無

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

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