Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查SQL数据库游标中的列是否为空?_Java_Android_Cursor - Fatal编程技术网

Java 如何检查SQL数据库游标中的列是否为空?

Java 如何检查SQL数据库游标中的列是否为空?,java,android,cursor,Java,Android,Cursor,我有一个数据库,他们可以在其中一个字段的图像中输入。图像保存在手机上,URI保存在数据库中 当用户决定不为某个项目添加图像时,我遇到了麻烦 我试图编写一个if/else语句,其中“if”条件检查游标的image列是否为null。我就是不知道在if条件中输入什么 下面是我正在使用的代码。基本上我想这样做,如果光标的列_WINE _IMAGE为空,就干杯吧。如果不为空,则设置图像 //This section turns the database's image URI stored in this

我有一个数据库,他们可以在其中一个字段的图像中输入。图像保存在手机上,URI保存在数据库中

当用户决定不为某个项目添加图像时,我遇到了麻烦

我试图编写一个if/else语句,其中“if”条件检查游标的image列是否为null。我就是不知道在if条件中输入什么

下面是我正在使用的代码。基本上我想这样做,如果光标的列_WINE _IMAGE为空,就干杯吧。如果不为空,则设置图像

//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 
改用这个:

if(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE) == null)
更新:

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

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

试着这样做:

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();
    }

但问题是,它只获取该索引的整数,对吗?除非我弄错了。无论哪种方式,当我尝试这种方式时,它有一个红色下划线,上面写着:运算符“==”不能应用于“int”,“null”,这非常有效。谢谢。我还不太擅长想出那样的方法。我希望我能在这方面有所改进。谢谢你提供这样的方法。现在我有一个很好的例子来说明我应该如何思考这样的项目。当你还在学习(我们不是真的)时,尽量不要使用这样的复合语句:
Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN\u WINE\u IMAGE))
:很难看出哪里出了问题,哪里出了问题以及原因。将所有内容分解,以便可以检查每个值。