简体   繁体   English

在Android中使用rawQuery和rowid从sqlite数据库中选择一行

[英]Selecting one row from sqlite database using rawQuery and rowid in Android

I've created an external SQLite database that looks something like this: 我创建了一个看起来像这样的外部SQLite数据库: 在此处输入图片说明

What I want to do in my Android application is to load all the values from one row using ROWID and put them into array. 我要在Android应用程序中执行的操作是使用ROWID从一行中加载所有值并将其放入数组中。 For example: 例如:

Cursor cursor = db.rawQuery("SELECT * FROM Dairy WHERE ROWID = 1", null);

This would return all the values from row 1: 51, 35, 63. However, this always returns just first value, in this case 51. Also, the cursor.getCount() always returns 1. Here's my complete code: 这将返回行1:51、35、63的所有值。但是,它始终仅返回第一个值,在本例中为51。而且,cursor.getCount()始终返回1。这是我的完整代码:

db = getReadableDatabase();

Cursor cursor = db.rawQuery("SELECT * FROM Dairy WHERE ROWID = 1", null);

yData = new int[cursor.getCount()];

if (cursor.moveToFirst()) {
    for (int i = 0; i < cursor.getCount(); i++) {
        yData[i] = cursor.getInt(0);
        cursor.moveToNext();
    }
    cursor.close();
}
db.close();

Thanks for help. 感谢帮助。

If I correctly understand you need to return value of every column from the single row. 如果我正确理解,则需要返回单行中每一列的值。 Since you select only single row and want to get value from every column you need only to get the index of every column. 由于您只选择单行,并且希望从每一列中获取价值,因此只需要获取每一列的索引即可。 and next iterate through them. 然后遍历它们。 Here is the code how it can be done. 这是代码如何完成的。

Cursor cursor = db.rawQuery("SELECT * FROM Dairy WHERE ROWID = 1 Limit 1", null);

    if (cursor.moveToFirst()) {

    String[] columnNames = cursor.getColumnNames();

     yData = new int[columnNames.length];

        for (int i = 0; i < columnNames.length; i++) {

            // Assume every column is int

            yData[i] = cursor.getInt(cursor.getColumnIndex(columnNames[i]));
        }

    }
    cursor.close();
    db.close();

The getInt (int columnIndex) method returns the requested column and not the entire row. getInt(int columnIndex)方法返回所请求的列,而不是整个行。 Whereas, moveToFirst () and moveToNext () will move the cursor to the first or next row if the cursor is not empty or if the cursor is not past the last row if moveToNext () is called. 而如果光标不为空,或者如果调用moveToNext()时光标未超过最后一行,则moveToFirst()moveToNext()会将光标移至第一行或下一行。 Otherwise both will return false. 否则,两者都将返回false。

So, on success, a cursor contains a complete row, and you can access the elements either by provide 0 based indices directly or getting a column index by getColumnIndex(String columnName) providing a column name to get the index of that column and accessing that column then. 因此,成功后,游标包含完整的行,您可以通过直接提供基于0的索引来访问元素,也可以通过getColumnIndex(String columnName)提供列名来获取该列的索引并访问该索引来获取列索引列。

You can refer to the android developers guide, Cursor . 您可以参考android开发人员指南Cursor

You always get the same value: 您总是得到相同的值:

if (cursor.moveToFirst()) {

  for (int i = 0; i < cursor.getCount(); i++) {
      //cursor.getInt(0); You probably want to get 0, 1 & 2
      yData[i] = cursor.getInt(0);
      cursor.moveToNext();
  }

cursor.close();

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM