dimanche 14 juin 2015

How to create a Unique column across three tables in Android SQLite?

I have three tables in my SQLite Database. In which a column named uSerialNo is present. I want that column to be unique. It is unique in same table. But how to make it unique across three different tables. A uSerialNo once inserted in a table cannot be repeated again. How to do that? I have three separate activities to insert to three tables but it's pretty much same. My Code is

DatabaseHelper is

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 carTable(userName TEXT UNIQUE, mobileNo INTEGER UNIQUE, address TEXT UNIQUE, uSerialNo INTEGER PRIMARY KEY, uImeiNo integer, uCimiNo INTEGER UNIQUE, uContactNo INTEGER, dealerCode INTEGER, dealerContactNo INTEGER)");
    Toast.makeText(context,"Database Created",Toast.LENGTH_SHORT).show();
    Log.i("dbcreate", "Database Created");
    Log.i("Table Created","Car Tracker Table Created");

    db.execSQL("CREATE TABLE bikeTable(userName TEXT UNIQUE, mobileNo INTEGER UNIQUE, address TEXT UNIQUE, uSerialNo INTEGER PRIMARY KEY, uImeiNo integer, uCimiNo INTEGER UNIQUE, uContactNo INTEGER, dealerCode INTEGER, dealerContactNo INTEGER)");
    Log.i("Table Created", "Bike Tracker Table Created");

    db.execSQL("CREATE TABLE cvTable(userName TEXT UNIQUE, mobileNo INTEGER UNIQUE, address TEXT UNIQUE, uSerialNo INTEGER PRIMARY KEY, uImeiNo integer, uCimiNo INTEGER UNIQUE, uContactNo INTEGER, dealerCode INTEGER, dealerContactNo INTEGER)");
    Log.i("Table Created","CV Tracker Table 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);
}

}

Insertion is done using

public void onClick(View v) {
            String strSno=etSno.getText().toString();
            String strImei=etImei.getText().toString();
            String strCimi=etCimi.getText().toString();
            String strDeviceContact=etDeviceContact.getText().toString();
            String strDealerCode=etDealerCode.getText().toString();
            String strDealerNo=etDealerNo.getText().toString();

            if (strSno.equals("")||strImei.equals("")||strCimi.equals("")||strDeviceContact.equals("")||strDealerCode.equals("")||strDealerNo.equals("")){
                show("Error","Please enter All the Fields");
            }else {
                putDatasToDatabase(strSno, strImei, strCimi, strDeviceContact, strDealerCode, strDealerNo);
                show("Success", "Data's Inserted Successfully");
                etSno.setText("");
                etImei.setText("");
                etCimi.setText("");
                etDeviceContact.setText("");
                etDealerCode.setText("");
                etDealerNo.setText("");
            }
 private void putDatasToDatabase(String strSno, String strImei, String strCimi, String strDeviceContact, String strDealerCode, String strDealerNo) {
    databaseHelper=new DatabaseHelper(this);
    SQLiteDatabase db=databaseHelper.getWritableDatabase();
    ContentValues cv=new ContentValues();

    try{
        cv.put("uSerialNo", strSno);
        cv.put("uImeiNo", strImei);
        cv.put("uCimiNo", strCimi);
        cv.put("uContactNO", strDeviceContact);
        cv.put("dealerCode", strDealerCode);
        cv.put("dealerContactNo", strDealerNo);

        db.insert("cvTable", null, cv);
        db.close();
    }catch (SQLiteException e){
        e.printStackTrace();
        Toast.makeText(AddCv.this,"Serial Number Already Entered. Check again and try later",Toast.LENGTH_LONG).show();
    }
}

Aucun commentaire:

Enregistrer un commentaire