https://developer.android.com/training/data-storage/sqlite?hl=ko#ReadDbRow
여기 예제를 보고 따라하는 중인데 어찌저찌 데이터 입력은 했습니다.
이제 읽어와야 하는데 커서를 이용해서 전체데이터를 불러오고 싶은데요
어떻게 해야할지 감이 오질 않습니다.
public void readAllDb(){
SQLiteDatabase db = dbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
BaseColumns._ID,
AssetListData.AssetEntry.COLUMN_NAME_ASSETNUMBER,
AssetListData.AssetEntry.COLUMN_NAME_ITEMNUMBER,
AssetListData.AssetEntry.COLUMN_NAME_PHONENAME,
AssetListData.AssetEntry.COLUMN_NAME_STATUS
};
// Filter results WHERE "title" = 'My Title'
String selection = AssetListData.AssetEntry.COLUMN_NAME_ASSETNUMBER + " = ?";
// String[] selectionArgs = { "My Title" };
// How you want the results sorted in the resulting Cursor
String sortOrder =
AssetListData.AssetEntry.COLUMN_NAME_ITEMNUMBER + " DESC";
Cursor cursor = db.query(
AssetListData.AssetEntry.TABLE_NAME, // The table to query
projection, // The array of columns to return (pass null to get all)
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
List itemIds = new ArrayList<>();
int i = 0;
while(i < cursor.getColumnCount()) {
// System.out.println("모든 데이터 "+i+"번째 : "+cursor.getString(i));
long itemId = cursor.getLong(
cursor.getColumnIndexOrThrow(AssetListData.AssetEntry._ID));
itemIds.add(itemId);
}
cursor.close();
}
이런 식으로 짜놨긴 헀는데 이걸 어떻게 출력을 해야하나요?
SELECT * FROM TABLE 이게 전체를 불러오는 걸로 알고 있는데 이 문법에 맞게 코드로 구현이 맞는건가요?
마지막 커서 코드 부분이 출력인거 같은데 itemIds 리스트 여기에 넣어준 걸 보여주면 되는건가요?
바로 보여주지 않고 리스트를 거쳐야 하는건지요.
저걸 돌려보면
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.foundphone, PID: 9517
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 727
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:468)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
at com.example.foundphone.Parsing.readAllDb(Parsing.java:97)
at com.example.foundphone.Parsing$Description.doInBackground(Parsing.java:274)
at com.example.foundphone.Parsing$Description.doInBackground(Parsing.java:184)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
이런 에러가 발생하고 에러나는 부분이 마지막 while문안에 이부분입니다.
long itemId = cursor.getLong(cursor.getColumnIndexOrThrow(AssetListData.AssetEntry._ID));
예제를 봐도 잘 되질 않네요 ㅠ