2016-05-26 50 views
0

我在Android studio中有一個應用程序,我從外部數據庫中獲取一些數據。所以我想在某些情況下獲取特定數據,並且在我的databaseHelper中使用了一個if語句實際上並不適用於我。你可以看到的if語句public List getListMbs()。當我打開應用程序時,它實際上只是從表中獲取所有東西,所以if不起作用。Android Studio無法獲得正確的sql查詢

public class DatabaseHelper extends SQLiteOpenHelper{ 
public static final String DBNAME = "mydb.sqlite"; 
public static final String DBLOCATION = "/data/data/com.hfad.myapp/databases/"; 
private Context mContext; 
private SQLiteDatabase mDatabase; 

public DatabaseHelper(Context context) { 
    super(context, DBNAME, null, 1); 
    this.mContext = context; 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

} 

public void openDatabase() { 
    String dbPath = mContext.getDatabasePath(DBNAME).getPath(); 
    if(mDatabase != null && mDatabase.isOpen()) { 
     return; 
    } 
    mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE); 
} 

public void closeDatabase() { 
    if(mDatabase!=null) { 
     mDatabase.close(); 
    } 
} 

public List<Cases> getListCases() { 
    Cases caze= null; 
    List<Cases> casesList = new ArrayList<>(); 
    openDatabase(); 
    Cursor cursor = mDatabase.rawQuery("SELECT * FROM Cases", null); 
    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()) { 
     caze = new Cases(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), ListCases.pose); 
     casesList.add(caze); 
     cursor.moveToNext(); 
    } 
    cursor.close(); 
    closeDatabase(); 
    return casesList; 
} 

public List<Motherboards> getListMbs() { 
    Motherboards mbs = null; 
    List<Motherboards> mbsList = new ArrayList<>(); 
    openDatabase(); 
    String query = ("SELECT * FROM Motherboards"); 
    if(ListCases.mboardsize == "mATX"){ 
     query="SELECT * FROM Motherboards WHERE Size ='mATX'"; 
    } 
    if(ListCases.mboardsize == "ATX") 
    { 
     query="SELECT * FROM Motherboards WHERE Size ='ATX'"; 
    } 
    Cursor cursor = mDatabase.rawQuery(query, null); 
    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()) { 
     mbs = new Motherboards(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), mainmenu.motherboard_btn_state); 
     mbsList.add(mbs); 
     cursor.moveToNext(); 
    } 
    cursor.close(); 
    closeDatabase(); 
    return mbsList; 
} 


} 
+1

那不是你如何在Java中比較字符串。去谷歌上查詢。 – 323go

+1

@ 323go oh my god!非常感謝!!!!! :P我找到了 – pkkr

回答

0

錯誤比較

==  

- >

.equals("string");