简体   繁体   中英

Check whether the column in SQLite NULL or not NULL

I have two tables, one is Info and another is WorkDetails .

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

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

For the row in WorkDetails , only the row that has value will be inserted. 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 . It will search from the fourth row, check whether there exists TimeOut value. If not, it will check the third and so on. If TimeOut value found in third row, it will insert the TimeOut value into Table info and will not check for second and first. Same goes to the TimeIn_info , the difference is TimeIn_info start searching from first row, not the fourth row.

  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?

How can I check whether the column is NULL or NOT NULL? If it is NULL, then TimeOut value will be inserted into TimeOut_Info, otherwise it will not! 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.

 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.

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. Passing null should be ok in your case. Have a look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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