【问题标题】:cursor.moveToFirst() returning false even though data is present in table即使表中存在数据, cursor.moveToFirst() 也会返回 false
【发布时间】:2016-03-21 13:05:26
【问题描述】:

我有一个名为“products3”的表,其中包含“id”、“type”、“width”、“length”、“thickness”、“quantity”..(按顺序)和“type”以外的列所有其他列都是“int” 我希望通过过滤其他列上的数据来选择数量并返回它。 即使数据存在于表中,方法“findproduct”中的光标在 if 条件下返回 false 或 NULL 我已经使用 SQLite 的 DB 浏览器浏览了表!请帮忙

    public class MyDBHandler extends SQLiteOpenHelper {

    int s;
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "productDB3.db";
    private static final String TABLE_PRODUCTS = "products3";

    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_TYPE = "type";
    public static final String COLUMN_QUANTITY = "quantity";
    public static final String COLUMN_WIDTH = "width";
    public static final String COLUMN_LENGTH = "length";
    public static final String COLUMN_THICK = "thick";

    public MyDBHandler(Context context, String name,
                       SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +
                TABLE_PRODUCTS + "("
                + COLUMN_ID + " INTEGER PRIMARY KEY autoincrement," + COLUMN_TYPE
                + " TEXT," + COLUMN_WIDTH + " INTEGER," + COLUMN_LENGTH + " INTEGER," + COLUMN_THICK + " INTEGER,"
                + COLUMN_QUANTITY + " INTEGER" + ")";
        db.execSQL(CREATE_PRODUCTS_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
                          int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
        onCreate(db);

    }

    public void addProduct(String type,int width,int length,int thick,int quant) {


        ContentValues values = new ContentValues();
        values.put(COLUMN_TYPE,type);
        values.put(COLUMN_WIDTH,width);
        values.put(COLUMN_LENGTH,length);
        values.put(COLUMN_THICK,thick);
        values.put(COLUMN_QUANTITY,quant);

        SQLiteDatabase db = this.getWritableDatabase();

        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }

    public int findProduct(String type,int width,int length,int thick) {

        String query = "SELECT quantity FROM products3 WHERE type='" + type +"' AND width=" + width + " AND length=" +
                length+" AND thick=" + thick + ";";
        SQLiteDatabase db = this.getWritableDatabase();

        if (cursor.moveToFirst()) {

            cursor.moveToFirst();

           s=cursor.getInt(0);

            cursor.close();
            return s;
        }
        db.close();
        return 0;
       }

    public boolean deleteProduct(String type,int width,int length,int thick) {



        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM products3 WHERE type='" + type +"' and width=" + width + " and length=" +
                length+" and thick=" + thick );
        db.close();
           }
}

【问题讨论】:

  • 添加错误日志。
  • 请跟踪堆栈
  • @Suhail Jain 试试这个 SQLiteDatabase db = this.getReadableDatabase();
  • 您的第一栏是什么?是字符串吗:cursor.getString(0)
  • @Chol 不,不是

标签: android android-sqlite android-cursor android-cursoradapter


【解决方案1】:

如果您在调试 SQL 查询时遇到问题,Android 提供了一个方便的方法 DatabaseUtils.dumpCursorToString(),它将整个 Cursor 格式化为字符串。然后,您可以将转储输出到 LogCat 并查看查询结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多