android - 比较 SQLite 中的变量

标签 android

假设我想比较 sqlite 数据库中的变量,如果屏幕上显示的文本与其相同,则在屏幕上打印错误。我尝试了 if (info.getData == info.getData ) then print error
但这不起作用,如果有人知道如何做,请告诉我

    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

}

代码:

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;
}
}

最佳答案

在Java中,如果要比较字符串,则需要使用equals方法。对于你的情况,像这样:

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

同样,如果你不关心大小写,你可以这样做:

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

关于android - 比较 SQLite 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9855801/

相关文章:

android - 共享元素转换和 ContentTransition 延迟计时

android - 当我尝试从网站中提取文本时,TextView 截断了很多信息

java - 如何读取首选项 XML 文件?

android - 将 Actionbarsherlock 导入到 Eclipse 中

php - 选择当前用户的所有好友id?

javascript - Titanium Javascript 运行时错误

android - "No XML content. Please add a root view or layout to your document."

android - Room Dao LiveData 作为返回类型导致编译时错误

android - 我可以找出需要在屏幕上显示的字符串的 dp 或 px 大小吗?

java - 如何在 Android 中使用 RecyclerView?