【问题标题】:How do I check if a column from a SQL database cursor is empty?如何检查 SQL 数据库游标中的列是否为空?
【发布时间】:2017-10-07 22:14:37
【问题描述】:

我有数据库,他们可以在其中输入图像作为字段之一。图片保存到手机上,URI保存到数据库中。

当用户决定不为某件商品添加图片时,我遇到了麻烦。

我正在尝试编写 if/else 语句,其中“if”条件检查光标的图像列是否为空。我只是不知道在 if 条件中输入什么内容。

这是我正在使用的代码。基本上我想这样做,如果光标的 COLUMN_WINE_IMAGE 为空,只需祝酒。如果不为null,则设置图片。

//This section turns the database's image URI stored in this cursor into a bitmap, and then sets the ImageView.
    Bitmap bitmap = null;
    try {
        if (............){
            Toast.makeText(this, "No image was taken for this item. (TOAST IS JUST TESTING PURPOSES)", Toast.LENGTH_SHORT).show();
        }else{
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE))));
            mFullImage.setImageBitmap(bitmap);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

这是我尝试过但没有成功的一个例子:

if (Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE))).equals(null)){ 

//This returns error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jeremy.sqlwine/com.example.jeremy.sqlwine.DetailsActivity}: java.lang.NullPointerException: uriString 

【问题讨论】:

    标签: java android cursor


    【解决方案1】:

    在单独的方法中检索列值。然后在使用前检查返回值。

    试试这样的:

    private String checkDatabaseValue(Cursor cursor){
        String result = null;
        try{
            //This can throw an error if the column is not found
            int index = cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE);
            result = cursor.getString(index).trim();
        }
        catch(Exception ex){
            // determine what threw the error and handle appropriately
            Log.e(TAG, ex.getMessage());
        }
        return result;
    }
    

    然后在这里使用它:

    Bitmap bitmap = null;
        try {
            // I'm assuming you have the cursor from somewhere!!
            String imageFile = checkDatabaseValue(cursor);
            if (imageFile == null){
                Toast.makeText(this, "No image was taken for this item. (TOAST IS JUST TESTING PURPOSES)", Toast.LENGTH_SHORT).show();
                return;
            }
    
            if(!imageFile.isEmpty()){
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(imageFile));
                mFullImage.setImageBitmap(bitmap);
            else{
                Toast.makeText(this, "Empty String. (TOAST IS JUST TESTING PURPOSES)", Toast.LENGTH_SHORT).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    【讨论】:

    • 这非常有效。非常感谢。我还不太擅长想出那样的方法。我希望我能在这方面有所改进。感谢您提供这样的方法。现在我有一个很好的例子来说明我应该如何思考这样的项目。
    • 当你还在学习的时候(我们不都是,真的)尽量不要使用这样的复合语句 :: Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE))) :: 很难看出哪里出了问题,哪里出了问题错了,为什么。分解所有内容,以便检查每个值。
    【解决方案2】:

    改用这个:

    if(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE) == null)
    

    更新:

    cursor.isNull(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE))
    

    【讨论】:

    • 这个问题是它只是获取该索引的整数,对吗?除非我弄错了。无论哪种方式,当我尝试这种方式时,它都有一个红色下划线,上面写着: Operator "==" cannot be applied to 'int', 'null'
    猜你喜欢
    • 2013-02-07
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    相关资源
    最近更新 更多