问题的因素:
调试了一上午,我要判断数据库表的name字段是否为空,使用了TextUtils.isEmpty(nameStr);来判断name字段是否为空,明明数据库是没有值,却一直显示有值,然后还去质疑TextUtils.isEmpty(nameStr);,最后才发现是我存入数据的时候有问题 不能 name + ""
TextUtils.isEmpty(nameStr)没有问题, 是存入的时候 name + "" 会导致数据库表字段是null
以下代码的解释:
注意:⚠️ 由于在真实开发的项目中代码非常庞大/非常复杂,也不能暴露公司的代码,所以以下代码都是简单的测试代码:
问题的示范:
package liudeli.my_work_summary; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; public class MainActivity02 extends Activity { private final String TAG = MainActivity02.class.getSimpleName(); private final String TABLE_NAME = "MainActivity02_TABLE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main02); MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "MainActivity02_DBName", null, 1); // 模拟插入数据 String name = null; String body = null; SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("body", body); values.put("name", name + ""); /** 注意:⚠️ 我这里是 name+"" */ db.insert(TABLE_NAME, null, values); // 不能在这里关闭db.close();,因为以下代码还需要db // 判断数据库表里面的name是否为空 Cursor cursor = db.query(TABLE_NAME, new String[]{"body","name"}, null, null, null, null,null); if (cursor.moveToFirst()) { String nameStr = cursor.getString(cursor.getColumnIndex("name")); if (TextUtils.isEmpty(nameStr)) { new AlertDialog.Builder(this) .setTitle("提示") .setMessage("name是空的") .setPositiveButton("我知道了", null) .show(); } else { new AlertDialog.Builder(this) .setTitle("提示") .setMessage("name是有值的") .setPositiveButton("我指定了", null) .show(); } } // 关闭db db.close(); } class MySQLiteOpenHelper extends SQLiteOpenHelper { public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { String createTableSQL = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, " + "body VARCHAR," + "name VARCHAR);"; try { db.execSQL(createTableSQL); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, TAG + "创表异常:" + e.toString()); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } }
解决方案一:在判断的时候以null作为空的标记来判断,不推荐此方法
package liudeli.my_work_summary; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; public class MainActivity02 extends Activity { private final String TAG = MainActivity02.class.getSimpleName(); private final String TABLE_NAME = "MainActivity02_TABLE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main02); MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "MainActivity02_DBName", null, 1); // 模拟插入数据 String name = null; String body = null; SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("body", body); values.put("name", name + ""); /** 注意:⚠️ 我这里是 name+"" */ db.insert(TABLE_NAME, null, values); // 不能在这里关闭db.close();,因为以下代码还需要db // 判断数据库表里面的name是否为空 Cursor cursor = db.query(TABLE_NAME, new String[]{"body","name"}, null, null, null, null,null); if (cursor.moveToFirst()) { String nameStr = cursor.getString(cursor.getColumnIndex("name")); // if (TextUtils.isEmpty(nameStr)) { if ("null".equals(nameStr)) { /** 注意:⚠️ 这种解决方案会被所有安卓程序员鄙视 */ new AlertDialog.Builder(this) .setTitle("提示") .setMessage("name是空的") .setPositiveButton("我知道了", null) .show(); } else { new AlertDialog.Builder(this) .setTitle("提示") .setMessage("name是有值的") .setPositiveButton("我指定了", null) .show(); } } // 关闭db db.close(); } class MySQLiteOpenHelper extends SQLiteOpenHelper { public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { String createTableSQL = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, " + "body VARCHAR," + "name VARCHAR);"; try { db.execSQL(createTableSQL); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, TAG + "创表异常:" + e.toString()); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } }
结果:
解决方案二:存入数据的时候,千万不能加入 name + ""
package liudeli.my_work_summary; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; public class MainActivity02 extends Activity { private final String TAG = MainActivity02.class.getSimpleName(); private final String TABLE_NAME = "MainActivity02_TABLE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main02); MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "MainActivity02_DBName", null, 1); // 模拟插入数据 String name = null; String body = null; SQLiteDatabase db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("body", body); values.put("name", name); /** 注意:⚠️ 这种解决方案才是正确的 不能加 + "" */ db.insert(TABLE_NAME, null, values); // 不能在这里关闭db.close();,因为以下代码还需要db // 判断数据库表里面的name是否为空 Cursor cursor = db.query(TABLE_NAME, new String[]{"body","name"}, null, null, null, null,null); if (cursor.moveToFirst()) { String nameStr = cursor.getString(cursor.getColumnIndex("name")); if (TextUtils.isEmpty(nameStr)) { new AlertDialog.Builder(this) .setTitle("提示") .setMessage("name是空的") .setPositiveButton("我知道了", null) .show(); } else { new AlertDialog.Builder(this) .setTitle("提示") .setMessage("name是有值的") .setPositiveButton("我指定了", null) .show(); } } // 关闭db db.close(); } class MySQLiteOpenHelper extends SQLiteOpenHelper { public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { String createTableSQL = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (ID INTEGER PRIMARY KEY, " + "body VARCHAR," + "name VARCHAR);"; try { db.execSQL(createTableSQL); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, TAG + "创表异常:" + e.toString()); } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } }
结果: