2011-10-19 122 views
3

我有一個數據庫,我將在解析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(); 
+0

薩米爾:不明白?? – Vinoth

回答

4

創建一個將保存數據的對象,例如, ClientData
從數據庫

public List<ClientData> selectAll() { 
    List<ClientData> list = new ArrayList<ClientData>(); 
    Cursor cursor = this.myDataBase.query(TABLE_NAME, new String[] { "userID, clientName" }, 
    null, null, null, null, null); 
    if (cursor.moveToFirst()) { 
     do { 
      list.add(new ClientData(cursor.getString(0), cursor.getString(1))); 
     } while (cursor.moveToNext()); 
    } 
    if (cursor != null && !cursor.isClosed()) { 
     cursor.close(); 
    } 
    return list; 
} 

獲取的所有數據執行插入語句之前創建的方法,獲取所有數據,然後檢查是否存在數據:

if (!list.contains(clientData)) { 
    executeInsert(); 
} 

我不知道是否SQLite支持存儲過程,但是如果存在的話,你也可以爲它寫一個存儲過程。

1

1)如果你在尋找的唯一ID不是讓你的ID字段自動遞增,並插入唯一的名字值

2)如果你不尋找獨特比檢索數組中的此表格存儲的所有數據要比將您插入的值與數據庫中的現有值進行比較

1

在您的示例表:

當您使用以下(使用SQLiteOpenHelper)中創建表,你可以設置你的名字作爲唯一的(如果這是你想要什麼獨特)。

String createPlayerTable = "create table " + 
       TIME_REPORT + 
       " (" + 
       USER_ID + " integer primary key autoincrement not null," + 
       CLIENT_NAME + " text not null," +      
       "UNIQUE("+CLIENT_NAME+")"+ 
       ");"; 

然後在插入insertIntoDatabase方法使用的

db.insertOrThrow(TIME_REPORT, null, initialValues); 

代替

db.insert(TIME_REPORT, null, initialValues); 

這可能拋出SQLiteConstraintException所以你必須添加一個try/catch。

希望這是你所需要的。

相關問題