我有一個數據庫,我將在解析JSON響應後填充項目。如何檢查數據庫中是否存在這些值,並防止再次插入?如何檢查Android中的數據庫中是否已經存在一個值
例如,數據庫的名稱:Exicom.sqlite
表的名稱:TimeReport
領域在數據庫:用戶ID,CLIENTNAME
我使用SQLite在android系統。
感謝您的輸入
這是我的HELPER類。我跳過了打開和關閉數據庫,因爲它太長了。隨後我將調用該方法將值插入到數據庫中。
public static final String indexNo = "index_no";
public static final String user_id = "userId";
public static final String company_id = "companyId";
public static final String user_name = "username";
public static final String client_Id = "clientId";
public static final String project_Id = "projectId";
public static final String report_Id = "reportId";
public static final String niv_1 = "niv1";
public static final String niv_2 = "niv2";
public static final String work_type_id = "workTypeId";
public static final String time_type_id = "timeTypeId";
public static final String date_id = "dateId";
public static final String month_id = "monthId";
public static final String year_id = "yearId";
public static final String hourS = "hours";
public static final String private_comment = "privateComment";
public static final String Ncomment = "comment";
public static final String mod_flag = "modFlag";
public static final String new_flag = "newFlag";
public static final String open_flag = "openFlag";
public static final String delete_flag = "deleteFlag";
private static final String DATABASE_NAME = "CopernicusDB.sqlite";
private static final String DATABASE_TABLE = "TimeReportTable";
private static final int DATABASE_VERSION = 1;
private final Context context;
private static DatabaseHelper DBHelper;
private static SQLiteDatabase db;
private static String TAG = "##---SecondActivityUserHelper---##";
public SecondActivityUserHelper(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.v(TAG,"The DatabaseHelper method ");
}
public long insertIntoDatabase(String companyId,String username, String dateId,
String clientId,String projectId,String niv1,String niv2,String workTypeId,String timeTypeId,
String hours,String comment,String privateComment,String openFlag,String reportId)
{
ContentValues initialValues = new ContentValues();
initialValues.put(company_id, companyId);
initialValues.put(user_name, username);
initialValues.put(date_id,dateId);
initialValues.put(client_Id,clientId);
initialValues.put(project_Id,projectId);
initialValues.put(niv_1,niv1);
initialValues.put(niv_2,niv2);
initialValues.put(work_type_id,workTypeId);
initialValues.put(time_type_id,timeTypeId);
initialValues.put(hourS, hours);
initialValues.put(Ncomment, comment);
initialValues.put(private_comment,privateComment);
initialValues.put(open_flag, openFlag);
initialValues.put(report_Id,reportId);
Log.v(TAG, "Inserted into database sucessfully");
return db.insert(DATABASE_TABLE, null, initialValues);
}
}
useradapter.openDatabase();
long id = dB.insertIntoDatabase( newcompanyid,newusername,newdate,
newClientId,newprojectId,newniv1,newniv2,newworktypeid,newtimetypeid,
newhours,newcomment,newprivatecomment,newopen,newreportid);
useradapter.closeDatabase();
薩米爾:不明白?? – Vinoth