我有一个数据库,我将在解析JSON响应后填充项目。如何检查数据库中是否已存在这些值并阻止再次插入?
EG。数据库名称:Exicom.sqlite
表名:TimeReport
数据库中的字段:userID,clientName
我在android中使用SQLite。
感谢您的输入
这是我的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 :(得分: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;
}
在执行insert语句之前,获取所有数据,然后检查数据是否存在:
if (!list.contains(clientData)) {
executeInsert();
}
我不确定SQLite是否支持存储过程,但如果确实如此,您也可以为此编写存储过程。
答案 1 :(得分:1)
1)如果您要查找唯一ID,请将您的id字段设为自动增量并仅插入名称值
2)如果你不是在寻找唯一的,而不是在数组中检索这个表存储的所有数据,那么将插入的值与数据库中的现有值进行比较
答案 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。
希望这就是你所需要的。