Hashey100
Hashey100

Reputation: 984

Comparing variables in SQLite

Lets say i wanted to compare a variable in the sqlite database and if the text displayed on the screen is identical to it then print a error on the screen. i tried if (info.getData == info.getData ) then print error
But that did not work , if anyone has any idea how to do it please inform me

    TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
    HotOrNot info  = new HotOrNot(this);
    info.open();
    String data = info.getData();
    info.close();
    tv.setText(data);
if(info.getData().equals(info.getData())){
 tv.setText("hey");
// this prints error

}

Code:

package f.s.l;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class HotOrNot {
public static final String KEY_ROWID ="_id";
public static final String KEY_DAY ="persons_day";

private static final String DATABASE_NAME ="HotOrNotdb";

private static final String DATABASE_TABLE ="peopleTable";
private static final int DATABASE_VERSION =1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +

            KEY_DAY + " TEXT NOT NULL);"


            );


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
    onCreate(db);

}

}
public HotOrNot(Context c){
ourContext =c;
}
public HotOrNot open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){

ourHelper.close();
}


public long createEntry1(String day) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();

cv.put(KEY_DAY, day);


return ourDatabase.insert(DATABASE_TABLE, null, cv);

}



 public String getData() {
// TODO Auto-generated method stub
String [] columns = new String[]{KEY_DAY};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";

int iDay = c.getColumnIndex(KEY_DAY);

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {


    result = result +c.getString(iDay) +"\n";
}


return result;
}
}

Upvotes: 2

Views: 326

Answers (2)

Barak
Barak

Reputation: 16393

First, I don't understand your code...

if(info.getData().equals(info.getData())) is like saying if(1 = 1)... it's never going to be false.

That aside, here is another issue:

info.close(); 
tv.setText(data); 
if(info.getData().equals(info.getData()))

You close the database and then try to run a method that requires it to be open. Get rid of the info.close() and things might work a bit better.

Upvotes: 0

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

In Java, if you are comparing strings you need to use equals method. In your case, like this:

if (info.getData().equals(info.getData()) {
    // do something
}

Similarly, if you don't care about casing, you can do:

if (info.getData().equalsIgnoreCase(info.getData()) {
    // do something
}

Upvotes: 2

Related Questions