如何检查列是否已有值?可以用光标找到吗?
我有两个表,一个是表info
,另一个是表workDetails
表格信息:ID(PK), Name, Weather, Date, Status, TimeIn_Info, TimeOut_Info
表工作详细信息:ID(PK),Project,WorkDescription,Per,TimeIn,TimeOut // 4 row
我的WorkDetails中有4行,我只希望插入只有值的行。对于TimeOut_Info
,它将来自表TimeOut
中的WorkDetails
。如果第4行为空,它将转到第3行。如果第3行中存在TimeOut
值,它将插入表Info并且不会查找第2行和第1行....
public void checkRow2(String a, String b, String c, String d) {
if ((TextUtils.isEmpty(a)) && (TextUtils.isEmpty(b)) && (TextUtils.isEmpty(c)) && (TextUtils.isEmpty(d))) {
// Toast.makeText(getApplicationContext(), "Doss", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Row 2 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))) {
WD.insertWorkDetails(a2, 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);
Toast.makeText(getApplicationContext(), "No Per2", Toast.LENGTH_LONG).show();
}
我附加的编码else-if
路径虽然已满足条件,但不起作用。我想知道检查所选列是否已有值的方法是什么?
Cursor cur = databaseb.rawQuery("SELECT COlumn COUNT(*) FROM +MyDatabaseHelper.TABLE_INFO+ ", null);
if (cur.getCount() > 0){
//do nothing everything's as it should be
}
else{
//
}
我尝试了另一种方法,但仍然没有运气:(
答案 0 :(得分:0)
有点难以弄清楚你要做什么,但是如果你有一个User
表,由UserId
键入,带有可选(可为空)UserDescription
,并且您想要检查特定用户是否有描述, SQL 这样做可以是以下之一:
-- Return non-null value if user has description, null if not, or no record if user not found
SELECT UserDescription
FROM User
WHERE UserId = ?
-- Return 1 if user has description, 0 if not, or no record if user not found
SELECT CASE WHEN UserDescription IS NULL THEN 0 ELSE 1 END AS HasDescription
FROM User
WHERE UserId = ?
-- Return 1 if user has description, or no record if not or if user not found
SELECT 1
FROM User
WHERE UserId = ?
AND UserDescription IS NOT NULL
-- Return 1 if user has description, or 0 if not or if user not found
SELECT COUNT(*)
FROM User
WHERE UserId = ?
AND UserDescription IS NOT NULL
还有更多方法可以做到。