How to dump results from SQLite Database

时间:2016-11-12 21:58:36

标签: android sqlite

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?

1 个答案:

答案 0 :(得分:1)

我不是专家,但试试这个。你的吐司应该一个接一个地扔名字。确保将名称更改为您自己的字段。

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();

            }