cursor
检查特定列是否为null。如果为null,则该值将插入到选定列中。代码实际上工作正常,但它们被插入到两个不同的行中。如何解决这个问题,以便将值c
和d
插入同一行但不同的列?
public void checkRow1(String a, String b, String c, String d)
{
WD.insertWorkDetails(a1, a, b, c, d);
Cursor mCursor = database.rawQuery("SELECT TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO + " WHERE " +
MyDatabaseHelper.TimeOut_Info + "= '" + d + "'", null);
if (mCursor == null) {
database=dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MyDatabaseHelper.TimeOut_Info, d);
database.insert(MyDatabaseHelper.TABLE_INFO, null, values);
return ;
}
database=dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MyDatabaseHelper.TimeIn_Info, c);
database.insert(MyDatabaseHelper.TABLE_INFO, null, values);
Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_LONG).show();
return;
}
被修改
为了检查特定行中是否已有值,我使用**COUNT**
public void checkRow3(String a, String b, String c, String d) {
if ((TextUtils.isEmpty(a)) && (TextUtils.isEmpty(b)) && (TextUtils.isEmpty(c)) && (TextUtils.isEmpty(d))) {
// Toast.makeText(getApplicationContext(), "Do", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Row 3 is empty", Toast.LENGTH_LONG).show();
return;
} else if ((a != null && a.trim().length() > 0) && ((TextUtils.isEmpty(b)) && (c != null && c.trim().length() > 0) && (d != null && d.trim().length() > 0))) {
// Toast.makeText(getApplicationContext(), "Done1", Toast.LENGTH_LONG).show();
WD.insertWorkDetails(a3, a, b, c, d);
Cursor mCursor = database.rawQuery("SELECT COUNT(TimeOut_Info) FROM " + MyDatabaseHelper.TABLE_INFO ,null);
if (mCursor.getCount()>0) {
Toast.makeText(getApplicationContext(), "Already has value", Toast.LENGTH_LONG).show();
return;
}
else
{
database = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MyDatabaseHelper.TimeOut_Info, d);
database.insert(MyDatabaseHelper.TABLE_INFO, null, values);
Toast.makeText(getApplicationContext(), "Inserted Time Out", Toast.LENGTH_LONG).show();
}
} else if ((a != null && a.trim().length() > 0) && (TextUtils.isEmpty(b)) && (TextUtils.isEmpty(c)) && (TextUtils.isEmpty(d))) {
WD.insertWorkDetails(a3, a, b, c, d);
// Toast.makeText(getApplicationContext(), "Row 3 is empty", Toast.LENGTH_LONG).show();
}
但即使特定列 null ,我也会"Already has a value"
。这会发生什么?
答案 0 :(得分:0)
要检查列是否具有空值,请使用方法isNull。
答案 1 :(得分:0)
rawQuery()
始终返回游标。如果未找到任何数据,则此光标将为空,即moveToFirst()将返回false
。
使用SELECT COUNT(...) FROM ...
的简单查询将始终返回包含单个值的单行,因此光标的getCount()
的返回值始终为1
。< / p>
请注意,COUNT()会计算行;要检查列是否为NULL,您必须使用WHERE子句。
要获取计数值,您必须从光标(使用moveToFirst()和getInt(0))实际读取它,或使用为您执行此操作的helper function:
long timeInfos = DatabaseUtils.longForQuery(database,
"SELECT COUNT(*) FROM " + MyDatabaseHelper.TABLE_INFO +
" WHERE TimeOut_Info IS NOT NULL", null);
if (timeInfos > 0) {
...