2013-08-01 13 views
6

Ho due tabelle: datiL'uso della parola chiave in SQLite Android

1) Employee -- _id, employee_name. 
2) Salary -- _id, amount, emp_id. 

Esempio:

Employee: 
1 John 
2 Rocky 
3 Marry 

Salary: 

1 500 1 //salary for John 
2 400 1 //salary for John 
3 600 2 //salary for Rocky 
4 700 2 //salary for Rocky 
5 350 3 //salary for Marry 

Ora, voglio cercare nella tabella degli stipendi per visualizzare l'quale ho pagato lo stipendio . diciamo che se cerco 'John' nella tabella degli stipendi, dovrebbe restituire le righe 1 e 2 che sono per John.

Ecco quello che sto cercando:

String where = " emp_id in (select _id from Employee where employee_name like ?)"; 

String[] whereArgs = new String[] {"'%" + mFilter + "%'" }; 

Cursor c = getDB(mContext).query("Salary", null, where, whereArgs, 
       null, null, null); 

ma è sempre di ritorno non ha prodotto risultati. Per favore aiuto.

Aggiornamento

ho il debug del codice, e query seguente trovato è in esecuzione nella cursore:

SELECT * FROM Salary WHERE emp_id in (select _id from Employee where employee_name like ?); 

risposta

2

args di selezione vengono utilizzati automaticamente come stringhe. Modificare questa:

String[] whereArgs = new String[] {"'%" + mFilter + "%'" }; 

a questo:

String[] whereArgs = new String[] {"%" + mFilter + "%" }; (removed ' from your strings) 

Con in più ' in là si annulla il testo e diventa ''%John%'' Il costruttore gestisce automaticamente ' per i args di selezione.

EDIT
modificare la tua ricerca anche a questo:

String sql = "SELECT * FROM Salary WHERE emp_id in (select _id from Employee where employee_name like ?)"; 
Cursor c = getDB(mContext).rawQuery(sql, whereArgs); 

EDIT 2

Ho ricreato la configurazione con la classe di sotto e tutto il mio codice eseguito bene. Ho estratto dall'utente John e ho ottenuto entrambi i risultati. Credo che il problema sia nella creazione del tuo database o semplicemente non hai dati all'interno del tuo database. Usa DDMS per estrarre il database e aprirlo con SQLite Browser. Controlla se il tuo database contiene dati al suo interno. Se lo fa, i tipi di creazione della tabella non corrispondono all'istruzione select. Quando chiamo GetMyValues ​​() ottengo 2 record restituiti dal cursore.

public class DataBaseHandler extends SQLiteOpenHelper { 

    private static final String TAG = "DBHandler"; 

    //Database VERSION 
    private static final int DATABASE_VERSION = 2; 

    //DATABASE NAME 
    private static final String DATABASE_NAME = "test"; 

    //DATABASE TABLES 
    private static final String TABLE_SALARY = "Salary"; 
    private static final String TABLE_EMP = "Employee"; 

    //DATABASE FIELDS 
    private static final String SalaryID= "_id"; 
    private static final String SalaryEmpName = "employee_name"; 
    private static final String EmpID= "_id"; 
    private static final String EmpAmt = "amount"; 
    private static final String EmpSalID = "emp_id"; 

    //DATABASE TYPES 
    private static final String INTPK = "INTEGER PRIMARY KEY"; 
    private static final String INT = "INTEGER"; 
    private static final String TEXT = "TEXT"; 

    //CREATE TABLES 
    private static final String CREATE_SALARY_TABLE = "CREATE TABLE " + TABLE_SALARY + "(" 
       + EmpID + " " + INTPK + "," + EmpAmt + " " + INT + "," 
       + EmpSalID + " " + INT + ")"; 

     //CREATE TABLE Salary(_id INTEGER PRIMARY KEY,amount INTEGER,emp_id INTEGER) 

    private static final String CREATE_EMPLOYEE_TABLE = "CREATE TABLE " + TABLE_EMP + "(" 
       + SalaryID + " " + INTPK + "," + SalaryEmpName + " " + TEXT + ")"; 

     //CREATE TABLE Employee(_id INTEGER PRIMARY KEY, employee_name TEXT) 

    public DataBaseHandler(Context context){ 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(CREATE_EMPLOYEE_TABLE); 
     db.execSQL(CREATE_SALARY_TABLE); 

     insertEmployeeValues(db); 
     insertSalaryValues(db);  
    } 

    private void insertEmployeeValues(SQLiteDatabase db){ 
     ContentValues values = new ContentValues(); 
     values.put(SalaryEmpName, "John"); 
     db.insert(TABLE_EMP, null, values); 
     values.clear(); 
     values.put(SalaryEmpName, "Rocky"); 
     db.insert(TABLE_EMP, null, values); 
     values.clear(); 
     values.put(SalaryEmpName, "Marry"); 
     db.insert(TABLE_EMP, null, values); 
     values.clear(); 
    } 

    private void insertSalaryValues(SQLiteDatabase db){ 
     ContentValues values = new ContentValues(); 
     values.put(EmpAmt, 500); 
     values.put(EmpSalID, 1); 
     db.insert(TABLE_SALARY, null, values); 
     values.clear(); 
     values.put(EmpAmt, 400); 
     values.put(EmpSalID, 1); 
     db.insert(TABLE_SALARY, null, values); 
     values.clear(); 
     values.put(EmpAmt, 600); 
     values.put(EmpSalID, 2); 
     db.insert(TABLE_SALARY, null, values); 
     values.clear(); 
     values.put(EmpAmt, 700); 
     values.put(EmpSalID, 2); 
     db.insert(TABLE_SALARY, null, values); 
     values.clear(); 
     values.put(EmpAmt, 350); 
     values.put(EmpSalID, 3); 
     db.insert(TABLE_SALARY, null, values); 
     values.clear(); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMP); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_SALARY); 
     onCreate(db); 
    } 

    public int GetMyValues(){ 
     String mFilter = "John"; 
     String[] whereArgs = new String[]{"%" + mFilter + "%"}; 
     int count = 0; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     String where = " emp_id in (select _id from Employee where employee_name like ?)"; 
     Cursor c = db.query("Salary",null, where, whereArgs,null,null,null); 
     count = c.getCount(); 
     c.close(); 
     return count; 
    } 
} 

Dev Reference:

è possibile includere? S in selezione, che saranno sostituiti da valori da selectionArgs, in modo che essi appaiono nella selezione. I valori saranno legati da stringhe

+0

Ho provato a rimuovere il 'dal codice .. ma non ha funzionato neanche. – mudit

+0

@mudit Puoi provare cosa c'è nella mia sezione di modifica per vedere se funziona – ObieMD5

+0

ho provato a usare il 2 modo .. sta funzionando sul client sqlite di firefox ma non funziona su Android. – mudit

-2

con il cursore è meglio credo, Codice

Demo:

 Cursor c = db.query(TABLE_CONTACTS, columns, KEY_MOBILE + " like '%" 
      + mobno + "'", null, null, null, order); 
return c; 

verrà restituito il valore se l'istanza di tale valore è nel database! !

+0

Non dovresti pubblicare un codice demo che non ha nulla da fare con la domanda sopra quando tutto è fornito per realizzare il codice reale necessario. – ObieMD5

+0

Dato che non ho abbastanza reputazioni, non potrei dare quel contenuto come commento ... Ecco perché è in Answer Response! – Exceptional