vendredi 12 juin 2015

How to select empty columns and Assign to String in Android SQLite?

I have a SQLite database in my Android App. Which has few empty columns because i have to insert them later. My table after initial insertion look like this. enter image description here

Now i have to update them later. It works fine. Now i have to get the empty columns value and check them like if(column1.equals("")) in order to insert to empty columns only. But i don't know what's the query to get the empty column from table and assign it to string. My Code is

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String str=(String)parent.getItemAtPosition(position);

                String[] smsMessages=str.split("#");
                String address=smsMessages[0];
                String dCode=smsMessages[1];
                String user=smsMessages[2];
                String addrs1=smsMessages[3];
                String pin=smsMessages[4];
                String addrs=addrs1+","+pin;
                String sNo=smsMessages[5];

    SmsManager smsManager=SmsManager.getDefault();
             try{
                databaseHelper=new DatabaseHelper(this);
                SQLiteDatabase db=databaseHelper.getReadableDatabase();

                    Cursor cursor = db.rawQuery("select uSerialNo,dealerCode from unittable where uSerialNo='" + sNo + "'", null);
                    cursor.moveToFirst();
                    String srNo = cursor.getString(0);
                    String drCode = cursor.getString(1);
                 Cursor cursor1=db.rawQuery("select userName,mobileNo,address, from unittable where userName IS NULL", null);
                 cursor1.moveToFirst();
                 String uName=cursor1.getString(0);
                 String uMobile=cursor1.getString(1);
                 String uAddress=cursor1.getString(2);

                 Toast.makeText(ActivationActivity.this,uName+uMobile+uAddress,Toast.LENGTH_LONG).show();

                 if (uName.equals("")||uMobile.equals("")||uAddress.equals("")) {

                     if (sNo.equals(srNo) && dCode.equals(drCode)) {
                         String updateQueryUser = "update unittable set userName='" + user + "'where userName IS NULL AND uSerialNo='" + srNo + "'and dealerCode='" + drCode + "'";
                         String updateQueryMobile = "update unittable set mobileNo='" + address + "'where mobileNo IS NULL AND uSerialNo='" + srNo + "'and dealerCode='" + drCode + "'";
                         String updateQueryAddress = "update unittable set address='" + addrs + "'where address IS NULL AND uSerialNo='" + srNo + "'and dealerCode='" + drCode + "'";

                         db.execSQL(updateQueryUser);
                         db.execSQL(updateQueryMobile);
                         db.execSQL(updateQueryAddress);

                         Toast.makeText(ActivationActivity.this, "Database Updated", Toast.LENGTH_SHORT).show();
                         smsManager.sendTextMessage(address, null, "YES. ACTIVATE. Default Password is 1234", null, null);
                     } else {
                         Toast.makeText(ActivationActivity.this, "Failed. Datas Mismatch", Toast.LENGTH_LONG).show();
                         smsManager.sendTextMessage(address, null, "NO. Details or Incorrect. Check the details or Call the Customer Care", null, null);
                     }
                 }else {
                     Toast.makeText(ActivationActivity.this,"Failed. Data's Mismatch",Toast.LENGTH_LONG).show();
                     smsManager.sendTextMessage(address,null,"NO. Details or Incorrect. Check the details or Call the Customer Care",null,null);
                 }

            }catch (Exception e){
                e.printStackTrace();
                Toast.makeText(ActivationActivity.this,"Error",Toast.LENGTH_LONG).show();
                smsManager.sendTextMessage(address,null,"NO. Details or Incorrect. Check the details or Call the Customer Care",null,null);
             }
}

Any one please help. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire