简体   繁体   English

检查SQLite中的列是否为NULL

[英]Check whether the column in SQLite NULL or not NULL

I have two tables, one is Info and another is WorkDetails . 我有两个表,一个是Info ,另一个是WorkDetails

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

Table WorkDetails: ID(PK),Project,WorkDescription,Per,TimeIn,TimeOut // 4 row 表工作详细信息: ID(PK),Project,WorkDescription,Per,TimeIn,TimeOut // 4 row

For the row in WorkDetails , only the row that has value will be inserted. 对于WorkDetails的行,将仅插入具有值的行。 Otherwise, it will not be inserted. 否则,将不会插入。 Next, the TimeOut_Info in the table Info will used to get the value of TimeOut in the table WorkDetails . 接下来,表InfoTimeOut_Info将用于获取表WorkDetailsTimeOut值。 It will search from the fourth row, check whether there exists TimeOut value. 它将从第四行开始搜索,检查是否存在TimeOut值。 If not, it will check the third and so on. 如果没有,它将检查third ,依此类推。 If TimeOut value found in third row, it will insert the TimeOut value into Table info and will not check for second and first. 如果在第三行中找到TimeOut值,它将在表信息中插入TimeOut值,并且不会检查第二和第一。 Same goes to the TimeIn_info , the difference is TimeIn_info start searching from first row, not the fourth row. 同样, TimeIn_info有所不同,区别在于TimeIn_info从第一行开始搜索,而不是从第四行开始搜索。

  Button btn1=(Button)findViewById(R.id.button2);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                AlertDialog.Builder builder=new AlertDialog.Builder(WorkDetailsTable.this);
                builder.setTitle("Data Saved");
                builder.setMessage("Are you sure you want to save?");
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int ii) {
                        W1 = txtWork1.getText().toString();
                        W2 = txtWork2.getText().toString();
                        W3 = txtWork3.getText().toString();
                        W4 = txtWork4.getText().toString();
                        a1 = spinnerTra.getSelectedItem().toString();
                        a2 = spinnerTra2.getSelectedItem().toString();
                        a3 = spinnerTra3.getSelectedItem().toString();
                        a4 = spinnerTra4.getSelectedItem().toString();
                        P1 = per1.getText().toString();
                        P2 = per2.getText().toString();
                        P3 = per3.getText().toString();
                        P4 = per4.getText().toString();
                        //    long ab = ts.insertTimeSheet(name, weather, date, status);
                        // long cf= WF.insertWorkForce(subContractors, noPeople, noHours,ab);
                        if ((TextUtils.isEmpty(W4)) && (TextUtils.isEmpty(P4)) && (TextUtils.isEmpty(h)) && (TextUtils.isEmpty(i))) {
                            checkRow3(W3, P3, f, g);// check row 3;
                            checkRow2(W2, P2, d, e1);//check row 2;
                            checkRow1(W1, P1, b, c);
                        }
                        else if (!(TextUtils.isEmpty(i)))
                        {
                            WD.insertWorkDetails(a4, W4, P4, h, i);
                            database = dbHelper.getWritableDatabase();
                            ContentValues values = new ContentValues();
                            values.put(MyDatabaseHelper.TimeOut_Info, i);
                            database.insert(MyDatabaseHelper.TABLE_INFO, null, values);
                            checkRow3(W3, P3, f, g);// check row 3;
                            checkRow2(W2, P2, d, e1);//check row 2;
                            checkRow1(W1, P1, b, c);
                        }

                    }

                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int ii) {
                        dialog.dismiss();
                    }
                });
                builder.show();


            }
        });
    }

    public void checkRow3(String a, String b, String c, String d) {
         if ((TextUtils.isEmpty(a)) && (TextUtils.isEmpty(b)) && (TextUtils.isEmpty(c)) && (TextUtils.isEmpty(d))) {

        }


       else if ((!(TextUtils.isEmpty(a)))||(!(TextUtils.isEmpty(b)))||(!(TextUtils.isEmpty(c)))&&((TextUtils.isEmpty(d)))) {
            WD.insertWorkDetails(a3, a, b, c, d);

         }
        else if ((!(TextUtils.isEmpty(a)))||(!(TextUtils.isEmpty(b)))||(!(TextUtils.isEmpty(c)))&&(!(TextUtils.isEmpty(d))))
         {
             WD.insertWorkDetails(a3, a, b, c, d);
             database = dbHelper.getWritableDatabase();
             ContentValues values = new ContentValues();
             values.put(MyDatabaseHelper.TimeIn_Info, d);
             database.insert(MyDatabaseHelper.TABLE_INFO, null, values);


         }

        return;
    }



   public void checkRow2(String a, String b, String c, String d)
    {
        if ((TextUtils.isEmpty(a)) && (TextUtils.isEmpty(b)) && (TextUtils.isEmpty(c)) && (TextUtils.isEmpty(d)))
        {
            return ;
        }
        else
            {
                WD.insertWorkDetails(a2, a, b, c, d);
            }
        return;

    }

    public void checkRow1(String a, String b, String c, String d)
    {
        WD.insertWorkDetails(a1, a, b, c, d);
        database = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(MyDatabaseHelper.TimeIn_Info, c);
        database.insert(MyDatabaseHelper.TABLE_INFO, null, values);
        return;
    }

As you can see my code is a little bit confusing. 如您所见,我的代码有些混乱。 Does it has any method that can search whether the column (TimeOut_info) already has a value? 它有什么方法可以搜索列(TimeOut_info)是否已经有值吗?

How can I check whether the column is NULL or NOT NULL? 如何检查列是NULL还是NOT NULL? If it is NULL, then TimeOut value will be inserted into TimeOut_Info, otherwise it will not! 如果为NULL,则将TimeOut值插入TimeOut_Info中,否则将不会! How can I achieve this? 我该如何实现?

When I attempt to use this method to check whether the specific column contains value or not, but I get rawQyery cannot be solved. 当我尝试使用此方法检查特定列是否包含值时,但是我无法解决rawQyery。

 public void checkRow2(String a, String b, String c, String d)
    {
        if ((TextUtils.isEmpty(a)) && (TextUtils.isEmpty(b)) && (TextUtils.isEmpty(c)) && (TextUtils.isEmpty(d)))
        {

        }
        else if ((!(TextUtils.isEmpty(a)))||(!(TextUtils.isEmpty(b)))||(!(TextUtils.isEmpty(c)))&&((TextUtils.isEmpty(d)))) {
            WD.insertWorkDetails(a3, a, b, c, d);

        }
        else if ((!(TextUtils.isEmpty(a)))||(!(TextUtils.isEmpty(b)))||(!(TextUtils.isEmpty(c)))||(!(TextUtils.isEmpty(d))))
        {

            database = dbHelper.getWritableDatabase();
            //Cursor mCursor=db.rawQuery("SELECT TimeOut_Info FROM "+MyDatabaseHelper.TABLE_INFO+"WHERE"+MyDatabaseHelper.TimeOut_Info+"=' "+d+"'");
            Cursor mCursor = database.rawQuery("SELECT TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO + " WHERE " +MyDatabaseHelper.TimeOut_Info + "= '" + d + "'");
            if(c==null)
            {

            }
        }
        return;

    }

Any suggestions? 有什么建议么? Thanks 谢谢

There is no overloaded method of rawQuery which takes one parameter. 没有使用一个参数的rawQuery重载方法。

Change 更改

Cursor mCursor = database.rawQuery("SELECT TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO + " WHERE " +MyDatabaseHelper.TimeOut_Info + "= '" + d + "'");

with

Cursor mCursor = database.rawQuery("SELECT TimeOut_Info FROM " + MyDatabaseHelper.TABLE_INFO + " WHERE " +
                MyDatabaseHelper.TimeOut_Info + "= '" + d + "'" , null);

you are already providing the selectionArgs, hardcoded in your query. 您已经在查询中提供了硬编码的selectionArgs。 Passing null should be ok in your case. 在您的情况下,传递null应该没问题。 Have a look here 在这里看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM