开发者

not able to get the id of the word from sqlite android

I am getting the word from the text view and passing to DB to get its id, i am able to get the word but not able to get the id. please look at my code and tell me what is the mistake and help me correct it

public void onClick(View v) {
        // TODO Auto-generated method stub
        TextView txtView = (TextView) v.findViewById(R.id.rightanswer);
        //Log.d("word " + txtView.getText(), "word");
        String word = txtView.getText().toString().trim();
        String words[] = new String[3];
        words = word.split(":");
        String selectedword = words[1].toLowerCase().trim();
        Log.d("Word", "|" + selectedword + "|");
        getWordId(selectedword);
    }
};

private int getWordId(String word) {
    // String selection = "Word=" + word;
    int id = -1;
    String query = "Select wordId from Words WHERE Word ='"+word+"'";
    //Log.d("Query ::" + query + "::", "Query");
    Cursor c = executeQuery(query);
    Log.d("Row Count", "" + c.getCount());
    if (c.moveToFirst()) {
        do {
            id = c.getInt(c.getColumnIndex("wordId"));
            Log.d("Id is:", "" + id);
            Intent intent = new Intent(getApplicationContext(), Word.class);
            intent.putExtra("wordid", id);
   开发者_如何学运维         intent.putExtra("page", "QuizResult");
            startActivity(intent);
            finish();
        } while (c.moveToNext());
    }
    c.close();
    return id;
}

i get the row id as 0. please help me


To get the row number from the database you should be getting the unique key (_id). Is wordId your unique key in the database? I althought thought it was important to android to name the column _id for whatever reason.


I bet that wordid is not INTEGER PRIMARY KEY therefore it returns 0. Check it at first.

Every row in table has it's own id with names rowid, oid, or _rowid_. Use it instead if you just need to distinguish rows. Or debug database in shell.

adb -e shell

cd /data/data/your.app.package.name/databases

sqlite3 database

.headers on

select * from words;

Above works on emulator and rooted devices

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜