使用Cursor检索SQLite数据库

时间:2020-12-26 08:40:45

I don't think I've been doing this right. A buddy told me to use the cursor.getColumnIndex() to retrieve the values of the items in that table column. Looks to me like it just returns an int value of the position of the column in the table which is why when I execute the code below I get either a 2.0 or 3.0 for my lat and long values as they are the second and third column in the table.

我不认为我这样做是对的。一个伙伴告诉我使用cursor.getColumnIndex()来检索该表列中项目的值。在我看来它只是返回表中列的位置的int值,这就是为什么当我执行下面的代码时,我得到的是我的lat和long值的2.0或3.0,因为它们是第二和第三列桌子。

What is the correct way to have this method return the items in the lat long columns instead of their position in the table?

使用此方法返回lat长列中的项而不是它们在表中的位置的正确方法是什么?

Here is what I was using with the getColumnIndex() :

以下是我在getColumnIndex()中使用的内容:

public List<LatLng> getAllPositions() {
     List<LatLng> positionList = new ArrayList<LatLng>();
      SQLiteDatabase database;
      ResumeMySQLiteHelper dbHelper;

      dbHelper = new ResumeMySQLiteHelper(this);
     database = dbHelper.getWritableDatabase();

     String[] allColumns = { ResumeMySQLiteHelper.COLUMN_ID,
              ResumeMySQLiteHelper.COLUMN_COMMENT, ResumeMySQLiteHelper.COLUMN_LAT, ResumeMySQLiteHelper.COLUMN_LONG };

     Cursor cursor = database.query(ResumeMySQLiteHelper.TABLE_NAME,
         allColumns, null, null, null, null, null);

     cursor.moveToFirst();
     while (!cursor.isAfterLast()) { 


         double latitude = cursor.getColumnIndex("lat");
         double longitude = cursor.getColumnIndex("long");
         String testString = Double.toString(latitude);
         String testLong = Double.toString(longitude);
         Log.w("myApp", testString + " " + testLong);

         //currently prints myApp 2.0 3.0 for every row in table

         LatLng newLatLng = new LatLng(latitude, longitude);
         positionList.add(newLatLng);
         cursor.moveToNext();
     }
     // make sure to close the cursor
     cursor.close();
     return positionList;
     }

2 个解决方案

#1


4  

This:

这个:

double latitude = cursor.getColumnIndex("lat");

should be:

应该:

double latitude = cursor.getDouble(cursor.getColumnIndex("lat"));

and the same for double longitude

和经度相同

#2


1  

int cloumnIndex = cursor.getColumnIndex("lat"); provides you index of the column that holds "lat" value after finding the index of that column you need to find value.

int cloumnIndex = cursor.getColumnIndex(“lat”);在找到需要查找值的列的索引后,为您提供包含“lat”值的列的索引。

cursor.getDouble(columnIndex);

#1


4  

This:

这个:

double latitude = cursor.getColumnIndex("lat");

should be:

应该:

double latitude = cursor.getDouble(cursor.getColumnIndex("lat"));

and the same for double longitude

和经度相同

#2


1  

int cloumnIndex = cursor.getColumnIndex("lat"); provides you index of the column that holds "lat" value after finding the index of that column you need to find value.

int cloumnIndex = cursor.getColumnIndex(“lat”);在找到需要查找值的列的索引后,为您提供包含“lat”值的列的索引。

cursor.getDouble(columnIndex);