如何检查 SQL 数据库游标中的列是否为空?
How do I check if a column from a SQL database cursor is empty?
我有数据库,他们可以在其中输入图像作为字段之一。图像保存到 phone,URI 保存到数据库。
当用户决定不为项目放入图像时,我遇到了麻烦。
我正在尝试编写 if/else 语句,其中 "if" 条件检查光标的图像列是否为空。我只是不知道要在 if 条件中输入什么。
这是我正在使用的代码。基本上我想做到这一点,如果光标的 COLUMN_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))
在单独的方法中检索列值。然后在使用前检查 return 值。
尝试这样的事情:
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();
}
我有数据库,他们可以在其中输入图像作为字段之一。图像保存到 phone,URI 保存到数据库。
当用户决定不为项目放入图像时,我遇到了麻烦。
我正在尝试编写 if/else 语句,其中 "if" 条件检查光标的图像列是否为空。我只是不知道要在 if 条件中输入什么。
这是我正在使用的代码。基本上我想做到这一点,如果光标的 COLUMN_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))
在单独的方法中检索列值。然后在使用前检查 return 值。
尝试这样的事情:
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();
}