comparing the record with the database record whether exists or not
I wrote the below code for compare the record with the database record.its comparing but inserting all the records
public void onClick(View v) {
if(v.equals(add))
{
if(ifExisting())
{
insert();
Log.e("Data Inserting","true");
Intent i=new Intent(AddCategory.this,ShareFolioActivity.class);
startActivity(i);
//Toast.makeText(AddCategory.this, "Already exists",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(AddCategory.this, "Already exists",Toast.LENGTH_LONG).show();
Log.e("Data Inserting","false");
}
}
}
boolean ifExisting() {
Log.e("wquery","SELECT * FROM sharelist WHERE category='"+category.getText().toString()+"'");
Cursor c = db.rawQuery( "SELECT category FROM sharelist WHERE category='"+category.getText().toString()+";'",null);
if(c.getCount()==-1)
{
Log.e("Condition true","true");
return false;
}
else
{
Log.e("Condition true","false");
return true;
开发者_开发技巧 }
}
You have if(ifExisting())
{
insert();
shouldn't it instead be if(!ifExisting())
{
insert();
Otherwise you are inserting if it exists.
You seem to be expecting that c.getCount()
will be -1 if the query returns 0 rows. Why wouldn't it return 0? (I don't remember seeing a case where it returns -1, but perhaps my memory is faulty.)
In any case, checking for c.getCount() <= 0
would seem to be safer.
Also, this query
Cursor c = db.rawQuery( "SELECT category FROM sharelist WHERE category='"+category.getText().toString()+";'",null);
is malformed -- look at the end where you have ";'"
. You want to have "'"
there. (Query strings that are passed to rawQuery
must not end in a semicolon; otherwise, "';"
would be the correct choice.)
As others have said to you on other threads, you really don't want to be making queries by concatenation; using the selectionArgs argument is a much safer way to pass query values.
精彩评论