Check if the row already exist in database in android

两盒软妹~` 提交于 2020-01-05 05:51:27

问题


please help me. I'm trying to make a registration app that will save consumer's data. The user needs to enter his complete name together with the company he wants to register to. He can register many times as long as it is in different company. My problem in my code below is that the program only compare the user input on the first data he enter. for example, the user enter his name and the company is ABC, the program accept it, when the user enter again his name and the company is DEF the program still accept it, but when the user enter his name and the company DEF again the program still accept it which is wrong, when you change the company in ABC the program did not accept it. So what I see is that my code only comparing the data on the first info entered.

here's my code:

in my databaseAdapter

public Constructor getNameCp(String name) 
{
    SQLiteDatabase db = this.getReadableDatabase();
    Constructor consName = new Constructor();
    try 
    { 
        Cursor c = db.query(Constants.DATABASE_TABLE_CONSUMER,null, Constants.CONSUMER_FULLNAME + "=?", new String[]{String.valueOf(name)},null, null, null);                                               
        if (c == null) 
        {                        
            return consName;                                   
        }

        else{    
            c.moveToFirst();               
            consName = new Constructor(Integer.parseInt(c.getString(0)),c.getString(c.getColumnIndex(Constants.CONSUMER_EMPNO)),c.getString(c.getColumnIndex(Constants.CONSUMER_FULLNAME)), c.getString(c.getColumnIndex(Constants.CONSUMER_CELLPHONENO)), c.getString(c.getColumnIndex(Constants.CONSUMER_COMPANY)));
        }                   
    }catch(Exception e){
        e.printStackTrace();
    }
    return consName; 
}

in my activity

String name = databaseAdapter.getNameCp(fullName).getConsumerFullname();
String comp = databaseAdapter.getNameCp(fullName).getCompanyname();

if ((fullName.equals(name)) && (companyName.equals(comp))){
    UpdateByEmployeeName(fullName);
    Toast.makeText(RegistrationActivity.this, "Account info already exist.", Toast.LENGTH_LONG).show();
}else{
    String currentDateandTime = sdf.format(new Date());
    databaseAdapter.SaveConsumerDetails(new Constructor(Emp.toUpperCase(), Lastname, Firstname, Middlename.toUpperCase(), Cp, Email, fullName, careFriend, companyName, emailmark, currentDateandTime, consumerId, consumercompanycode));
refresh();
}

回答1:


Use below method in your database class for getting list of all companies for particular employee name:

public ArrayList<String> getCompName(String name) {

    ArrayList<String> alCompNm = new ArrayList<String>();

    try {
        Cursor c = db.rawQuery("select CompName FROM projsitemast where Name='"+name+"'", null);
        if (c != null) {
            c.moveToFirst();
            for (int j = 0; j < c.getCount(); j++) {
                alCompNm.add(c.getString(0));
                System.out.println("Client Site Name : " + c.getString(0));
                c.moveToNext();
            }
            c.close();
        }
        return alCompNm;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return alCompNm;
}

In your Main Activity:

flag = 0;
ArrayList<String> cNm = new ArrayList<String>();
dba.open();
cNm = dba.getCompName(yourname);

for(int i=0;i< cNm.size();i++){
   String cmpname = cNm.get(i).toString();

   if(cmpname.equals(yourcmpname)){
           flag = 1;
      }
}

if(flag==0){
String currentDateandTime = sdf.format(new Date());
    databaseAdapter.SaveConsumerDetails(new Constructor(Emp.toUpperCase(), Lastname, Firstname, Middlename.toUpperCase(), Cp, Email, fullName, careFriend, companyName, emailmark, currentDateandTime, consumerId, consumercompanycode));
refresh();
// OR as per your requirement
}
else{
Toast.makeText(RegistrationActivity.this, "Account info already exist.", Toast.LENGTH_LONG).show();
}


来源:https://stackoverflow.com/questions/17160132/check-if-the-row-already-exist-in-database-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!