mardi 30 décembre 2014

SQLitle and WHERE - T9 dictionary

I have a problem with SQLite in Android and keyword WHERE.


Currently I am writing Android App using T9 dictionary. My table looks like this:



@Override
public void onCreate( SQLiteDatabase db ){
db.execSQL( "CREATE TABLE IF NOT EXISTS " + TABLE_WORDS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_CODE + " TEXT NOT NULL, " +
COLUMN_WORD + " TEXT NOT NULL, " +
COLUMN_FREQUENCY + " INTEGER NOT NULL, " +
"UNIQUE( " + COLUMN_WORD + "));"
);
}


Then I load some data to it. For example for word(COLUMN_WORD) "test" my code(COLUMN_CODE) in T9 dictionary is 8378 and for word "example" my code is equal to 3926753.


Next I write new SQL query:



sql = "SELECT `" + COLUMN_WORD+"`,`"+ COLUMN_CODE + "` FROM `" + TABLE_WORDS + "` LIMIT 10;";


And next a another query:



code = "2";
sql = "SELECT " + COLUMN_WORD+","+ COLUMN_CODE + " FROM " + TABLE_WORDS + " WHERE " + COLUMN_CODE + "='" + code + "';";


And in JAVA I write: Cursor cursor = database.rawQuery( sql, null );


Now in the first query results are correct, but in the second I do not get expected results.


Logs are here: http://oi57.tinypic.com/f78di.jpg


What am I doing wrong in the SQL query? I tried using:



  • WHERE code='2'

  • WHERE code="2"

  • WHERE code=2

  • WHERE code='2'

  • WHERE code="2"

  • WHERE code=2

  • WHERE code = '2'

  • WHERE code = "2"

  • WHERE code = 2

  • WHERE code like "2"

  • WHERE code =? and then cursor = database.rawQuery( sql, new String[] { code } );


All methods I have tried give me the same negative results. If you know how to solve this problem, please help me.


Best regards Kamil4u


Full code:



public List<String> getWords( String code ){
String word;
List<String> list = new ArrayList<String>();

String sql;

sql = "SELECT `" + COLUMN_WORD+"`,`"+ COLUMN_CODE + "` FROM `" + TABLE_WORDS + " WHERE " + COLUMN_CODE + "='" + code + "';";
Cursor cursor = database.rawQuery( sql, null );


Log.i("---TEXT---", sql );
if( cursor != null ){
cursor.moveToFirst();
while( cursor.moveToNext() ){
word = cursor.getString( cursor.getColumnIndex( COLUMN_WORD ) );
list.add( word );

Log.i("---TEXT---", "-->"+cursor.getString( cursor.getColumnIndex( COLUMN_WORD ) )+"<---->" + cursor.getString( cursor.getColumnIndex( COLUMN_CODE ) ) + "<--" );
}
}

Log.i("---TEXT---", "list( " + !list.isEmpty() + " )" + list );
list.clear();
sql = "SELECT `" + COLUMN_WORD+"`,`"+ COLUMN_CODE + "` FROM `" + TABLE_WORDS + "` LIMIT 10;";

Log.i("---TEXT---", sql );
cursor = database.rawQuery( sql, null );
if( cursor != null ){
cursor.moveToFirst();
while( cursor.moveToNext() ){
word = cursor.getString( cursor.getColumnIndex( COLUMN_WORD ) );
list.add( word );

Log.i("---TEXT---", "-->"+cursor.getString( cursor.getColumnIndex( COLUMN_WORD ) )+"<---->" + cursor.getString( cursor.getColumnIndex( COLUMN_CODE ) ) + "<--" );
}
}

Log.i("---TEXT---", "list( " + !list.isEmpty() + " )" + list );

return list;
}

Aucun commentaire:

Enregistrer un commentaire