sam sam - 5 months ago 7
Android Question

compare a string with a database column's values

I am developing an app in which i have a profiles list which is stored in a database containing too many profiles,the problem is that when i save a new profile, the app must have to check the profile is already exist or not....how to do that

mSaveProfile.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View view)
{
CProfileDataSource m_profileDataSource = new CProfileDataSource(CAddNewProfileActivity.this);
_profile = mProfileName.getText().toString();
if (_profile.matches(""))
{
Toast.makeText(CAddNewProfileActivity.this, "You did not enter a profileName", Toast.LENGTH_SHORT).show();
return;
} else if(_profile.equalsIgnoreCase(getProfileName(CAddNewProfileActivity.this)))
{
Toast.makeText(CAddNewProfileActivity.this, "Profile already exists", Toast.LENGTH_SHORT).show();
return;
}
else
{
userProfile.setProfileName(_profile);
m_profileDataSource.addProfile(new CUserProfile(CAddNewProfileActivity.this, userProfile.getProfileName(), userProfile.getBrightness(), userProfile.getSleepTime(), m_n8SensorState, userProfile.getOptimization()));
Toast.makeText(CAddNewProfileActivity.this, "Saved", Toast.LENGTH_SHORT).show();
}
finish();
}
});


and the following function is only checking the last entered profile not the whole list ..

public String getProfileName(CAddNewProfileActivity cAddNewProfileActivity){
String profile=null;
CProfileDataSource profileDataSource =new CProfileDataSource(cAddNewProfileActivity);
List<CUserProfile> profileName=profileDataSource.getAllProfiles();
for(CUserProfile cp:profileName){
profile=cp.getProfileName();
}
return profile;
}

sam sam
Answer
public long getProfilesRecordCount(String profileName)
{
 SQLiteDatabase db=this.openReadable(); 
long rawNum=DatabaseUtils.queryNumEntries(db,TABLE_NAME,"Profile_Name=?", new String[]{profileName});
 return rawNum; 
} 

rawNum should not greater than 0 ...if it is then String already exists.

Comments