2013-12-18 16 views
7

Ho una tabella Restaurants nel mio SQLite DB che hanno le seguenti recordcompletamento automatico Android con SQLite come opere parzialmente

| Name | Latin_Name | 
+--------+------------+ 
| Манаки | Manaki  | 
+--------+------------+ 
| Енрико | Enriko  | 
+---------------------+ 

Ora sto facendo la ricerca come questa:

Dal mio frammento:

String selection = DBHelper.COL_NAME + " LIKE ? OR " + 
        DBHelper.COL_LATIN_NAME + " LIKE ?"; 

String[] selectionArgs = {"%"+term+"%", "%"+term+"%"}; 

CursorLoader loader = new CursorLoader(getActivity(), 
           DatabaseContentProvider.REST_CONTENT_URI, 
           columns, selection, selectionArgs, null); 

Il fornitore di contenuti query metodo:

public Cursor query(Uri uri, String[] projection, String selection, 
        String[] selectionArgs, String sortOrder) { 

    db = dbHelper.getReadableDatabase(); 
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); 
    switch(URI_MATCHER.match(uri)){ 
     case REST_LIST: 
      builder.setTables(DBHelper.REST_TABLE); 
      break; 
     case REST_ID: 
      builder.appendWhere(DBHelper.COL_ID + " = " 
        + uri.getLastPathSegment()); 
      break; 
     default: 
      throw new IllegalArgumentException("Unsupported URI: " + uri); 
    } 
    Cursor cursor = builder.query(db, projection, selection, selectionArgs, 
      null, null, sortOrder); 


    cursor.setNotificationUri(getContext().getContentResolver(), uri); 

    return cursor; 
} 

Quindi piuttosto semplice, giusto? Ora qui viene il problema:

Se il term che viene in è en posso vedere il Enriko ristoranti tra gli altri.

Se mi passa enri non riesco a vedere la Enriko come un risultato più.

Stesso discorso per la Manaki ristorante posso vedere fino mana e dopo che (per manak termine per ex) non riesco a vederlo nella lista dei risultati.

Stavo eseguendo il debug del mio ContentProvider e ho realizzato che il cursore era vuoto, quindi il problema deve essere a livello di database, immagino.

Per favore aiuto.

UPDATE:

Avendo i commenti @laalto in mente ho deciso di fare un po 'di test sul database. Nel metodo onCreate() del mio SQLiteOpenHelper ho inserito solo quei due record nella tabella a mano e ha funzionato.

Ora il problema è che devo inserire 1300 record onCreate() da un file json spedito nella cartella assets. In questo momento sto analizzando il file json creando un ArrayList<Restaurant> quindi lo attraversiamo e inseriamo un record per oggetto per tutti i 1300 elementi.

Questo tipo di inserimento non funziona con il metodo SQLite LIKE.

Ci sono trucchi per questo tipo di popolamento del database?

Forse ho bisogno di cambiare la codifica dei file (ora è UTF-8) o forse le regole di confronto del database, o forse getString() da JSONObject possono essere ottimizzati per il database?

+0

Come si fa a produrre i 'Latin_Name' che va nella db? Pensando alla possibilità che ci siano dei caratteri che non sono visibili ma influenzano l'abbinamento 'LIKE'. – laalto

+0

Quando si esegue questa query 'SELECT * FROM Ristoranti dove Latin_name LIKE '% enri%'' in un'applicazione SQLite desktop ottengo il risultato desiderato. – djandreski

+0

Ok: è possibile controllare l'SQL generato dal builder. per esempio. sostituire 'builder.query()' con 'builder.buildQuery()', registrare la stringa di query e interrogare con 'db.rawQuery()'. – laalto

risposta

2

Se è urgente, ecco una soluzione rapida e sporca.

2.600 stringhe (1.300 cirillico + 1.300 latino) sono davvero pochi i valori per gestire in memoria: si può semplicemente caricare tutti i valori dalla SQLite con

select name, latin_name from restaurants 

e poi si può avvolgere il set di risultati in Java durante la ricerca per la stringa corrispondente con una stringa . contiene il metodo. Basta ricordare che contiene metodo è case sensitive, così si dovrebbe usare qualcosa di simile a questo:

string1.toLowerCase().contains(string2.toLowerCase()) 
1

documentazione SQLite link

The LIKE operator does a pattern matching comparison. The operand to the right of the LIKE 
operator contains the pattern and the left hand operand contains the string to 
match against the pattern. A percent symbol ("%") in the LIKE pattern matches any 
sequence of zero or more characters in the string. An underscore ("_") in the LIKE 
pattern matches any single character in the string. Any other character matches 
itself or its lower/upper case equivalent (i.e. case-insensitive matching). 
(A bug: SQLite only understands upper/lower case for ASCII characters by default. 
The LIKE operator is case sensitive by default for unicode characters that are 
beyond the ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ' LIKE 'Æ' 
is FALSE.) 
Problemi correlati