将字符串与数据库列的值进行比较

时间:2016-04-21 09:15:44

标签: java android sqlite

我正在开发一个应用程序,其中我有一个配置文件列表存储在包含太多配置文件的数据库中,问题是当我保存新配置文件时,应用程序必须检查配置文件是否已存在....怎么做

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

并且以下函数仅检查最后输入的配置文件而不是整个列表..

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

2 个答案:

答案 0 :(得分:0)

下面的示例方法说明了如何检查配置文件是否已存在。这是您必须根据您的更改值,表名和条件的样本。

 public void saveOrUpdateProfile(String profileId, String name, String emailId) {
        boolean UPDATE_FLAG = false;

        SQLiteDatabase sdb = this.getWritableDatabase();

        String selectQuery = "SELECT * FROM tableName WHERE profileId = '" + profileId + "';";

        Cursor cursor = sdb.rawQuery(selectQuery, null);
        try {
            if (cursor != null && cursor.getCount() > 0) {
                if (cursor.moveToFirst()) {
                    UPDATE_FLAG = true;
                }
            }

            ContentValues values = new ContentValues();
            values. ("name", name);
            values. ("email_id", emailId);

            if (UPDATE_FLAG) {
                sdb.update(tableName, values, "profileId = ?", new String[]{profileId});
            } else {
                sdb.insert(tableName, null, values);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
    }

答案 1 :(得分:0)

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

rawNum不应大于0 ......如果是,那么String已经存在。