2011-09-12 25 views
5

Sto sviluppando un'applicazione menu ristorante. La mia app ha una tabella SQLite che ha queste colonne:sqlite Android: come recuperare dati specifici da una particolare colonna?

"id", "category", "item_name" 

Contenuto della categoria colonna è di tipo stringa. La chiave primaria della tabella è id.

Desidero recuperare i dati di una particolare categoria.

Ad esempio, voglio recuperare i nomi degli articoli di tutti gli articoli che appartengono alla categoria Veg e quindi visualizzare questo risultato nella visualizzazione elenco.

Ho provato a seguire query diverse ma entrambe non funzionano. Mi aiuti per favore.

String vg ="Veg"; 
Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] { 
      KEY_ROWID, KEY_CATEGORY, KEY_NAME,KEY_PRIZE,KEY_DESCRIPTION }, 
     KEY_CATEGORY + "=" + vg , null, null, null,null, null); 
     if (mCursor != null) {    
         mCursor.moveToFirst(); 
     } 
     return mCursor; 

interrogazione Raw

String query = "SELECT * FROM todo WHERE category =" + vg ; 
      Cursor cursor = database.rawQuery(query,null); 
      if (cursor != null) { 
       cursor.moveToFirst(); 
      } 
      return cursor; 
+0

http://stackoverflow.com/questions/6653502/android-sqlite -come-ottenere-particolare-valore-colonna http://stackoverflow.com/questions/5974799/how-can-i-retrieve-a-particular-coloumn-from-sqlite-in-android –

risposta

14

provare questo:

String query = "SELECT * FROM todo WHERE category='" + vg; 

Cursor cursor = database.rawQuery(query,null); 

     if (cursor != null) { 
      cursor.moveToFirst(); 
     } 
     return cursor; 
+1

Grazie, anche questo funziona. 'mancava dopo "=" dalla mia richiesta. – sagar

9
String query = "SELECT item_name FROM todo WHERE category =" + vg ; 
Cursor cursor = database.rawQuery(query,null); 
if (cursor.moveToFirst()) { 
     while (cursor.isAfterLast() != true) { 
      string itemname = cursor.getString(cursor.getColumnIndex("item_name"))); 
      } 
} 

Qui moveToFirst controlla se ci sono elementi che soddisfano i criteri e poi scorre cursore utilizzando ciclo while. La stringa nome-oggetto può essere sostituita dalla lista adaper per popolare i dati. Spero che questo ti aiuti.

1

Prova questo:

String vg ="Veg"; 
return database.rawQuery("SELECT * FROM subjects where " + KEY_CATEGORY + " = ?", 
          new String[]{vg}); 
0
String q="SELECT * FROM todo WHERE category='" + vg +"'"; 
    SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = null; 
     cursor = db.rawQuery(q, null); 
     if (cursor.moveToFirst()) { 
      do { 

      } while (cursor.moveToNext()); 

     } 
5
dbHelper = new DBHelper(getApplicationContext()); 
SQLiteDatabase db = dbHelper.getReadableDatabase(); 

Cursor cursor = db.rawQuery("select * from centuaryTbl where email='"+email+"'",null); 
if (cursor.moveToFirst()) 
{ 
    do 
    { 
     String s1 = cursor.getString(cursor.getColumnIndex("s1")); 
     String s2 = cursor.getString(cursor.getColumnIndex("s2")); 
     String s3 = cursor.getString(cursor.getColumnIndex("s3")); 


    }while (cursor.moveToNext()); 
} 
0
String selectQuery = "select * from " + TABLE; 
     sqlDatabase = this.getWritableDatabase(); 

    Cursor cursor = sqlDatabase.rawQuery(selectQuery, null); 

    if (cursor != null) { 
     cursor.moveToFirst(); 
    } 


    return cursor; 

questo vi aiuterà a ..

1
String query = "SELECT * FROM Table_Name WHERE Col_Name='name'"; 
Cursor cursor = mDb.rawQuery(query, null); 
+0

Sebbene questo codice possa rispondere alla domanda, fornire un contesto aggiuntivo sul perché e/o su come questo codice risponde alla domanda migliora il suo valore a lungo termine. –

Problemi correlati