如何检查是否值已经存在于Android的数据库 [英] How to check if a value already exists in the database in Android
问题描述
我有,我将与项目填充后,我分析JSON响应的数据库。如何检查是否值已present在数据库中,prevent再次插入?
例如。数据库的名称:Exicom.sqlite
表的名称:TimeReport
字段在数据库:用户ID,客户端名
我使用的SQLite的机器人。
感谢您的输入
这是我的助手类。我已经跳过打开和关闭所述数据库,因为它会太长。跟进,我会打电话给插入值到数据库中的方法。
公共静态最后弦乐indexNo =index_no;
公共静态最后弦乐USER_ID =用户id;
公共静态最后弦乐的company_id =companyId;
公共静态最后弦乐用户名=用户名;
公共静态最后弦乐CLIENT_ID =客户端ID;
公共静态最后弦乐PROJECT_ID =projectId;
公共静态最后弦乐report_Id =reportId;
公共静态最后弦乐niv_1 =niv1;
公共静态最后弦乐niv_2 =niv2;
公共静态最后弦乐work_type_id =workTypeId;
公共静态最后弦乐time_type_id =timeTypeId;
公共静态最后弦乐DATE_ID =dateId;
公共静态最后弦乐month_id =monthId;
公共静态最后弦乐year_id =yearId;
公共静态最后弦乐小时=小时;
公共静态最后弦乐private_comment =privateComment;
公共静态最后弦乐Ncomment =意见;
公共静态最后弦乐mod_flag =modFlag;
公共静态最后弦乐new_flag =newFlag;
公共静态最后弦乐open_flag =openFlag;
公共静态最后弦乐delete_flag =deleteFlag;
私有静态最后弦乐DATABASE_NAME =CopernicusDB.sqlite;
私有静态最后弦乐DATABASE_TABLE =TimeReportTable;
私有静态最终诠释DATABASE_VERSION = 1;
私人最终上下文的背景下;
私有静态DatabaseHelper DBHelper;
私有静态SQLiteDatabase分贝;
私有静态字符串变量=## --- SecondActivityUserHelper --- ##;
公共SecondActivityUserHelper(上下文CTX)
{
this.context = CTX;
DBHelper =新DatabaseHelper(上下文);
}
私有静态类DatabaseHelper扩展SQLiteOpenHelper {
公共DatabaseHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
Log.v(TAG的DatabaseHelper法);
}
众长insertIntoDatabase(字符串companyId,字符串username,字符串dateId,
字符串客户端ID,字符串projectId,字符串niv1,字符串niv2,字符串workTypeId,字符串timeTypeId,
字符串时,字符串注释,字符串privateComment,字符串openFlag,字符串reportId)
{
ContentValues initialValues =新ContentValues();
initialValues.put(的company_id,companyId);
initialValues.put(用户名,用户名);
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(小时,分);
initialValues.put(Ncomment,评论);
initialValues.put(private_comment,privateComment);
initialValues.put(open_flag,openFlag);
initialValues.put(report_Id,reportId);
Log.v(TAG,插入到数据库中成功地);
返回db.insert(DATABASE_TABLE,空,initialValues);
}
}
useradapter.openDatabase();
长ID = dB.insertIntoDatabase(newcompanyid,newusername,newdate,
newClientId,newprojectId,newniv1,newniv2,newworktypeid,newtimetypeid,
newhours,newcomment,newprivatecomment,newopen,newreportid);
useradapter.closeDatabase();
创建一个对象,将保留您的数据,例如。 ClientData code>
为从数据库中获取的所有数据创建方法
公开名单< ClientData>全选() {
名单< ClientData>名单=新的ArrayList< ClientData>();
光标光标= this.myDataBase.query(TABLE_NAME,新的String [] {用户ID,CLIENTNAME},
NULL,NULL,NULL,NULL,NULL);
如果(cursor.moveToFirst()){
做 {
list.add(新ClientData(cursor.getString(0),cursor.getString(1)));
}而(cursor.moveToNext());
}
如果(光标=空&安培;!&安培;!cursor.isClosed()){
cursor.close();
}
返回列表;
}
在执行插入语句时,获取所有数据,然后检查是否存在数据:
如果(!list.contains(clientData)){
executeInsert();
}
我不知道是否SQLite支持存储过程,但如果这样做,你可以写一个存储过程也是如此。
I've a database that I will populate with items after I parse a JSON response. How can I check if the values are already present in the database and prevent inserting again ?
Eg. Name of Database: Exicom.sqlite
Name of table: TimeReport
Fields in Database: userID, clientName
I am using SQLite in android.
Thanks for your input
This is my HELPER class. I've skipped opening and closing the database as it will be too long. Followed up by where I will call the method to insert values into the database.
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();
Create an object that will hold your data, eg. ClientData
Create a method for fetching all data from the database
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;
}
Before executing your insert statements, fetch all data and then check if data exists:
if (!list.contains(clientData)) {
executeInsert();
}
I am not sure if SQLite supports stored procedures, but if it does, you could write a stored procedure for that as well.
这篇关于如何检查是否值已经存在于Android的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!