Reputation: 15
I have a wired problem. I write a code which use sqlite. This is the relevant lines:
private final String DB_NAME = "cus";
private final String TABLE_NAME = "cus2";
SQLiteDatabase customersDB2 = null;
/** Called when the activity is first created. */
private void mFillDbsTable(){
try {
customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 1, 'the ', 'ה- ');");
customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 2, 'of ', 'של ');");
customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 3, 'to ', 'אל ');");
customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 4, 'and ', 'ו - ');");
customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 5, 'a ', 'מופיע לפני שם עצם ביחיד ללא הא הידיעה ');");
customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 6, 'in ', 'בתוך ');");
customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 7, 'for ', 'עבור ');");
................
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learn);
try {
customersDB2 = this.openOrCreateDatabase( DB_NAME, MODE_PRIVATE, null);
customersDB2.execSQL("CREATE TABLE " +
TABLE_NAME +
" (Block INT," +
" Name VARCHAR, Street VARCHAR);");
mFillDbsTable();
}catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "create/Open the database problem");
}
Now, when I ask query:
correctAnswer=oneTOfour.nextInt(4);
Toast.makeText(this,"correctAnswerק"+correctAnswer,Toast.LENGTH_SHORT).show();
correctAnswerTHE_ANSWER =(r.nextInt(1000)) + 1;
Toast.makeText(this,"correctAnswerTHE_ANSWER"+correctAnswerTHE_ANSWER,Toast.LENGTH_SHORT).show();
ArrayList<String> results = new ArrayList<String>();
Cursor c = customersDB2.rawQuery("SELECT Name, Street FROM " +
TABLE_NAME +
" where Block = 66 ", null);
if (c != null ) { //true
Toast.makeText(this,"c.getColumnIndex(Name)" + c.getColumnIndex("Name"),Toast.LENGTH_SHORT).show(); //got 0!!
if (c.moveToFirst()) {
do {
name = c.getString(c.getColumnIndex("Name"));
street = c.getString(c.getColumnIndex("Street"));
results.add(name + ", " + street );
}while (c.moveToNext());
}
}
else{
Toast.makeText(this,"null2",Toast.LENGTH_SHORT).show();
}
question.setText(name); //this is null
}
It don't reach the second if
Maybe I first ran this app with 2 column only. I have tried to open other project, to change the name of the table..nothing work. Some ideas?
Upvotes: 1
Views: 124
Reputation: 694
Probably related to the UTF non-stardandard characters you have in your inserts. You should try inserting using ContentValues like:
ContentValues row = new ContentValues();
row.put("Name", "blah");
row.put("Street", "your utf strings");
row.put("Block", 1);
long id = db.insert(TABLE_NAME, null, row);
Upvotes: 0
Reputation: 22342
"SELECT Name, Street FROM " ...
When you name fields you want to select, it returns them in that order, so Name
is the first column, or 0
.
If you want them in the order you created them, try SELECT * FROM
instead.
The question is, what does it matter what order they're in? You're using c.getColumnIndex()
already, so their order is not meaningful.
Upvotes: 1