检查SQLite中的列是否为NULL或不为NULL

时间:2015-10-12 12:38:40

标签: java android sqlite

我有两个表,一个是Info,另一个是WorkDetails

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

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

对于WorkDetails中的行,只会插入具有值的行。否则,它将不会被插入。接下来,表TimeOut_Info中的Info将用于获取表TimeOutWorkDetails的值。它将从第四行搜索,检查是否存在TimeOut值。如果没有,它将检查third,依此类推。如果在第三行中找到TimeOut值,它会将TimeOut值插入表信息中,并且不会检查第二个和第一个。同样适用于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;
    }

正如您所看到的,我的代码有点令人困惑。它是否有任何方法可以搜索列(TimeOut_info)是否已有值?

如何检查列是NULL还是NOT NULL?如果为NULL,则TimeOut值将插入到TimeOut_Info中,否则不会!我怎样才能做到这一点?

当我尝试使用此方法检查特定列是否包含值时,但是我得到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;

    }

有什么建议吗?感谢

1 个答案:

答案 0 :(得分:2)

没有rawQuery的重载方法,它接受一个参数。

更改

Cursor mCursor = database.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 + "'" , null);

您已经在查询中提供了硬编码的selectionArgs。在你的情况下,传递null应该没问题。看看here