jeudi 11 juin 2015

How to prevent updation after one record inserted in to table in Android SQLite?

In my task I have a database table in which looks like in following figure enter image description here

I have left 3 Columns empty. Inputs for those three columns came via SMS. I shown the SMS in ListView. When I click those ListView items the remaining three columns are updated from that SMS. Filled table Looks like below figure enter image description here I have used update query which checks for uSerialNo column value and the same value came via SMS and updates it. No problem with that. If i send send SMS with already inserted uSerilaNo value it also updates the table like in the following figure. enter image description here I don't want to update anything after the values inserted for one 'uSerialNo'. It must send error to the number requesting it. I posted my code here My DatabaseHelper

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String db_name="unitdb";
public static final int version=1;
Context context;

public DatabaseHelper(Context context){
    super(context, db_name, null, version);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL("CREATE TABLE unittable(userName TEXT, mobileNo INTEGER UNIQUE, address TEXT, uSerialNo INTEGER UNIQUE, uImeiNo integer, uSimiNo INTEGER UNIQUE, uContactNo INTEGER, dealerCode INTEGER, dealerContactNo INTEGER)");
    Toast.makeText(context,"Database Created",Toast.LENGTH_SHORT).show();
    Log.i("dbcreate","Database Created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion>=newVersion)
        return;

    if (oldVersion==1){
        Log.d("New Version","Datas can be Upgraded");
    }
    Log.d("Sample Data", "onUpgrade:" + newVersion);

}

Code used for updation

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

                if (sNo.equals(srNo)&&dCode.equals(drCode)){
                    String updateQueryUser="update unittable set userName='"+user+"'where uSerialNo='"+srNo+"'and dealerCode='"+drCode+"'";
                    String updateQueryMobile="update unittable set mobileNo='"+address+"'where uSerialNo='"+srNo+"'and dealerCode='"+drCode+"'";
                    String updateQueryAddress="update unittable set address='"+addrs+"'where 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);
                }
            }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);
             }
}

Please Help me...

Aucun commentaire:

Enregistrer un commentaire