sam sam - 1 year ago 68
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 to do that

mSaveProfile.setOnClickListener(new View.OnClickListener()

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();
} else if(_profile.equalsIgnoreCase(getProfileName(CAddNewProfileActivity.this)))
Toast.makeText(CAddNewProfileActivity.this, "Profile already exists", Toast.LENGTH_SHORT).show();
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();

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){
return profile;

sam sam
Answer Source
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download