[英]Checking if row exists SQLite
I would like to check if any row exists within the Sqlite database. 我想检查Sqlite数据库中是否存在任何行。
my java class 我的java课
public static final String DATABASE_TABLE2 = "receivernumber";
public static final String KEY_ROWID2 = "hpnumberID2";
public static final String KEY_NAME2 = "hpNumber2";
public long insertContact2(String hpNumber2)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME2, hpNumber2);
if(CheckIsDataAlreadyInDBorNot(0) == true) {
return db.update(
DATABASE_TABLE2, initialValues, "SET='"+KEY_NAME2+"'" +"WHERE"+ "KEY_ROWID2="+1, null
) > 0;
}
else {
return db.insert(DATABASE_TABLE2, null, initialValues);
}
//if there is alrdy a record, create a method to reject intake
return 0;
}
public boolean CheckIsDataAlreadyInDBorNot(long hpnumberID) {
Cursor mCursor = db.query(
true, DATABASE_TABLE2, new String[] {KEY_ROWID, KEY_NAME},KEY_ROWID + "=" + hpnumberID, null, null, null, null, null
);
String Query = "Select * from " + DATABASE_TABLE2 + " where " + KEY_ROWID2 + " < " + 0;
SQLiteDatabase sqldb = EGLifeStyleApplication.sqLiteDatabase;
Cursor cursor = sqldb.rawQuery(Query, null);
if(cursor.getCount<=0) return false;
return true;
}
public boolean updateContact2(long hpnumberID2, String hpNumber2)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME2, hpNumber2);
//args.put(KEY_NAME3, Selected);
//return db.update(DATABASE_TABLE2, args, KEY_ROWID2 + "=" + hpnumberID2, null) > 0;
//db.execSQL("UPDATE " + DATABASE_TABLE2 + " SET " + KEY_NAME2 + " WHERE " + KEY_ROWID2 + "=1 ");
return db.update(DATABASE_TABLE2, args, "SET='" + KEY_NAME2 + "'" + "WHERE" + "KEY_ROWID2=" + 1, null) > 0;
}
So my database layout is such that the user can only add a number for the first time. 因此,我的数据库布局使用户只能第一次添加数字。 Subsequent times the user wishes to add a number, it would be replaced by an edit function instead. 随后,用户希望添加一个数字,它将被一个编辑功能代替。 However, there's an error called EGLifeStyleApplication cannot be resolved to a variable. 但是,存在一个名为EGLifeStyleApplication的错误,无法将其解析为变量。 However, as this is an answer from questions solved successfully, they did not really explain what is the function of that EGLifeStyleApplications. 但是,由于这是成功解决问题的答案,因此他们并未真正解释EGLifeStyleApplications的功能是什么。 So how do I go about doing what I want to achieve? 那么我该如何去做自己想实现的目标? (How do I edit my insert statement) Thanks. (如何编辑我的插入语句)谢谢。
Replace the offending line with 用替换违规行
sqldb = ctx.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
where ctx is a context you will pass as a parameter to your CheckIsDataAlreadyInDBorNot method. 其中ctx是上下文,您将作为参数传递给CheckIsDataAlreadyInDBorNot方法。 ie: 即:
public boolean CheckIsDataAlreadyInDBorNot(Context ctx, long hpnumberID) {
and DB_NAME is a string containing your db name, ie: DB_NAME是包含您的数据库名称的字符串,即:
private final static String DB_NAME = "rec_nums.db";
public static final String DATABASE_TABLE2 = "receivernumber";
public static final String KEY_ROWID2 = "hpnumberID2";
public static final String KEY_NAME2 = "hpNumber2";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.