Red Virus Red Virus - 21 days ago 7
Android Question

How to dump results from SQLite Database

I' am stuck on this from yesterday. Researching didn't help much because I just got handed over this project and I have no knowledge of Android.

myDB = null;
try {
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
File file = new File(dir, "database.db");
myDB = SQLiteDatabase.openDatabase(file.toString(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READONLY);

String q = "SELECT * FROM companies";
Cursor mCursor = myDB.rawQuery(q, null);

Context context = getActivity().getApplicationContext();
CharSequence text = mCursor.toString();
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

myDB.close();
} catch (SQLException e) {
e.printStackTrace();
}


From the toast I get
android.database.sqlite.SQLiteCursor@2f95e49b


How do I dump all the database results?

Answer

I' am not an expert but try this. Your toast should throw names one after the other. Make sure to change the name to your own field.

String q = "SELECT * FROM companies";
            Cursor mCursor = myDB.rawQuery(q, null);
            mCursor.moveToFirst();
            while ( !mCursor.isAfterLast()) {
                String name= mCursor.getString(mCursor.getColumnIndex("name"));
                mCursor.moveToNext();

                Context context = getActivity().getApplicationContext();
                CharSequence text = name;
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();

            }
Comments