4

I am fetching a string from Database using the column id. When I enter a query in SQLite DB Browser it returns what is need but the same query returns nothing when coded through Java.

My Data Base contains a table named drugs which has 3 columns i.e. drug_id, drug_name and drug_overview. Using drug_id i am fetching drug_overview. I have tried the query in db browser which returns me the correct string from drug_overview but the same query returns nothing when coded through java.

SQLite DB Browser query:

SELECT * FROM drugs Where drug_id = 50;

JAVA CODE:

String query105 = "SELECT * FROM drugs Where drug_id = " + drug_id;
                        Log.e("TESTDB1","Drugs table query: " + query105);
                        Cursor c105 = db.rawQuery(query105,null);

                        if (c105 != null){

                            while (c105.moveToNext()){

                                String overview = c105.getString(c105.getColumnIndexOrThrow("drug_overview"));
                                Log.e("TESTDB1","Overview: " + overview);

                            }

                            c105.close();

                        }

Expected result is Overview: Acyclovir is an antiviral drug. It slows the growth and spread of the herpes virus in the body. It will not cure herpes, but it can lessen the symptoms of the infection.Acyclovir is used to treat infections caused by herpes viruses, such as genital herpes, cold sores, shingles, and chicken pox, as well as varicella (chickenpox), and cytomegalovirus.Acyclovir may also be used for purposes not listed in this medication guide.

But the actual result is Overview: empty . When i change the id in my query it gives the correct result from a different drug.

13
  • "drug_overview" looks rather like a column name than it does a column_index Commented Mar 28, 2019 at 13:02
  • c105.getColumnIndex("drug_overview") returns the column index Commented Mar 28, 2019 at 13:04
  • did you check that your database on the device is correctly filled by removing the WHERE part? Commented Mar 28, 2019 at 13:06
  • Yes my database is not empty as i can fetch the name of the drug by the same query Commented Mar 28, 2019 at 13:08
  • You are missing this line SQLiteDatabase db = this.getReadableDatabase(); Commented Mar 28, 2019 at 13:11

2 Answers 2

0

I am afraid that your problem may be with the actual data itself as Mike said in comment, I think your database in the files is old and you haven't copied the latest to folder. Try to re-install and delete old database

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You Sir! I had to uninstall my app and then reinstall it with new database that's all!
That's great. Happy to know that your problem is resolved.
0

Your query returns 0 or 1 lines so I think you should use c105.moveToFirst() instead of c105.moveToNext(). moveToNext is supposed to be used for a list, not for a single entry. Do something like:

if (c105.moveToFirst()){
         String overview = c105.getString(c105.getColumnIndex("drug_overview"));
         // do something with the result                    
    }
c105.close();

14 Comments

Well i have multiple entries in it and i am fetching a list of drugs from it. So i had to use next. c105.moveToNext()
Multiple entries with the same drug_id?
Are you checking if c105.moveToNext() is true?
You can also try to use getColumnIndexOrThrow to see if your column name is mispelled
No multiple entries with different drug id's
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.