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.

Huzaifa Asif
  • 658
  • 1
  • 8
  • 24
  • "drug_overview" looks rather like a column name than it does a column_index – Stultuske Mar 28 '19 at 13:02
  • c105.getColumnIndex("drug_overview") returns the column index – Huzaifa Asif Mar 28 '19 at 13:04
  • did you check that your database on the device is correctly filled by removing the `WHERE` part? – Christian Mar 28 '19 at 13:06
  • Yes my database is not empty as i can fetch the name of the drug by the same query – Huzaifa Asif Mar 28 '19 at 13:08
  • You are missing this line SQLiteDatabase db = this.getReadableDatabase(); – primo Mar 28 '19 at 13:11
  • see this link https://stackoverflow.com/a/12299215/8101634 – primo Mar 28 '19 at 13:12
  • Well this is just another method of fetching data. I can already fetch data from my DB it's just the drug_overview column that returns nothing while it has data in it. – Huzaifa Asif Mar 28 '19 at 13:15
  • if no errors occurs, i would say 1- incorrect column name, 2 empty value in bd, could be empty for this field? how are you shipping data into your database on app? – Yazan Mar 28 '19 at 13:21
  • Column Name is also correct as it returns 2 when Column Id is fetched. And it also has data in that particular cell as i can view it in DB browser. I have my data base in my resources folder and i am using SQLite open helper to access data from it. – Huzaifa Asif Mar 28 '19 at 13:25
  • I have tried c105.getColumnIndexOrThrow() so that if column name is not correct it throws exception. – Huzaifa Asif Mar 28 '19 at 13:50
  • I suspect that your issue may be with the actual data itself, that is the database on the android device does not match the database you are using for DB Browser. I'd suggest re-copying the database to the resources folder, uninstalling the app and re-trying. – MikeT Mar 28 '19 at 20:32
  • @MikeT This worked! i was not refreshing the database installed in my phone! Due to which the previous database was being loaded. Thank you so much! You are a saviour. – Huzaifa Asif Mar 29 '19 at 10:50
  • @MikeT please post this as the answer – Huzaifa Asif Mar 29 '19 at 10:50

2 Answers2

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();
Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
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