如何检查列是否已有值?(How can I check whether the column already has value?)

如何检查列是否已有值? 可以用光标找到吗?

我有两个表,一个是表info ,另一个是表workDetails

表信息: ID(PK), Name, Weather, Date, Status, TimeIn_Info, TimeOut_Info

表WorkDetails: ID(PK),Project,WorkDescription,Per,TimeIn,TimeOut // 4 row

我的WorkDetails中有4行,我只希望插入只有值的行。 对于TimeOut_Info ,它将来自表WorkDetails TimeOut 。 如果第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{ // }

我尝试了另一种方法,但仍然没有运气:(

How can I check whether the column already has value? Can use cursor to find?

I have two tables, one is table info and another is table workDetails

Table Info: ID(PK), Name, Weather, Date, Status, TimeIn_Info, TimeOut_Info

Table WorkDetails: ID(PK),Project,WorkDescription,Per,TimeIn,TimeOut // 4 row

There are 4 row in my WorkDetails and I only want the row which has value only will be inserted. And for the TimeOut_Info, it will get from TimeOutwhich is in table WorkDetails. If row 4 is empty, it will goes for row 3. If there exits a TimeOut value in row 3, it will insert into table Info and will not look for row 2 and row 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(); }

The coding else-if path I attached doesn't work although it already meet the condition. I wonder is it the way to check whether the selected column already has a value?

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{ // }

I have tried another method, but still no luck :(

最满意答案

有点难以弄清楚你想要做什么,但是如果你有一个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
 

有更多的方法可以做到这一点。

A bit difficult to figure out what you are trying to do, but if you for example have a User table, keyed by UserId, with an optional (nullable) UserDescription, and you want to check if a particular user has a description, the SQL to do so can be one of the following:

-- 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
 

There are more ways to do it.

更多推荐