Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用游标查询我的数据库_Android_Sqlite_Sqliteopenhelper_Android Sqlite - Fatal编程技术网

Android 使用游标查询我的数据库

Android 使用游标查询我的数据库,android,sqlite,sqliteopenhelper,android-sqlite,Android,Sqlite,Sqliteopenhelper,Android Sqlite,我有一个数据库设置,我知道我插入它正确。然而,我很难通过查询从中返回任何数据。这是我的主类和数据库助手类 public class DBTesterActivity extends Activity { MyDBAdapter db; Cursor cursor; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { I

我有一个数据库设置,我知道我插入它正确。然而,我很难通过查询从中返回任何数据。这是我的主类和数据库助手类

public class DBTesterActivity extends Activity {

MyDBAdapter db;
Cursor cursor;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    Item item1;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    db = new MyDBAdapter(this);
    db.open();
    db.insertEntry(item1 = new Item("Bathtub", "Bathroom", "Typical", "Clean", "fill, wash", "Round, deep", "Bathroom", "Toilet, Bathroom", R.drawable.ic_launcher));
    //this.populateDatabase();
    //Cursor entryCursor = db.getAllEntries();
    item1 = db.getEntry(1);
    Log.i("db", item1.toString());
}
这里是我的数据库适配器类

public class MyDBAdapter {
private static final String DATABASE_NAME = "myDatabase.db";
private static final String DATABASE_TABLE = "mainTable";
private static final int DATABASE_VERSION = 1;

//main table columns
public static final String KEY_ITEM              = "itemName";
public static final String KEY_GROUP             = "itemGroup";
public static final String ITEM_CLASSIFICATION   = "classification";
public static final String KEY_USE               = "use";
public static final String KEY_ACTION            = "action";
public static final String KEY_PROPERTIES        = "properties";
public static final String KEY_ASSOCIATION       = "association";
public static final String KEY_IMG_ID            = "imgId";

// The index (key) column name for use in where clauses.
public static final String KEY_ID="_id";

// The name and column index of each column in your database.
public static final int NAME_COLUMN = 1;
public static final int GROUP_COLUMN = 2;
public static final int CLASSIFICATION_COLUMN = 3;
public static final int USE_COLUMN = 4;
public static final int ACTION_COLUMN = 5;
public static final int PROPERTIES_COLUMN = 6;
public static final int ASSOCIATION_COLUMN = 7;
public static final int IMG_ID_COLUMN = 8;
// TODO: Create public field for each column in your table.

// SQL Statement to create a new database.
private static final String DATABASE_CREATE = "create table " + 
        DATABASE_TABLE + " (" 
        + KEY_ID + " integer primary key autoincrement, " 
        + KEY_ITEM + " TEXT, " 
        + KEY_GROUP + " TEXT, " 
        + ITEM_CLASSIFICATION + " TEXT, " 
        + KEY_USE + " TEXT, " 
        + KEY_ACTION + " TEXT, " 
        + KEY_PROPERTIES + " TEXT, " 
        + KEY_ASSOCIATION + " TEXT, " 
        + KEY_IMG_ID + " INTEGER);";

// Variable to hold the database instance
private SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private myDbHelper dbHelper;

public MyDBAdapter(Context _context) {
    context = _context;
    dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public MyDBAdapter open() throws SQLException {
    try {  
        db = dbHelper.getWritableDatabase();
    } catch (SQLiteException e) {
        db = dbHelper.getReadableDatabase();
    }
    return this;
}

public void close() {
    db.close();
}

public void insertEntry(Item item) {
    // TODO: Create a new ContentValues to represent my row
    // and insert it into the database.
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_ITEM, item.get_item_name());
    values.put(KEY_GROUP, item.get_group());
    values.put(ITEM_CLASSIFICATION, item.get_item_classification());
    values.put(KEY_USE, item.get_use());
    values.put(KEY_ACTION, item.get_action());
    values.put(KEY_PROPERTIES, item.get_properties());
    values.put(KEY_ASSOCIATION, item.get_association());
    values.put(KEY_IMG_ID, item.get_img_id());

    // insert row to table
    try{
        db.insertOrThrow(DATABASE_TABLE, null, values);
        Log.i("success", "insert was successful");
    }catch (Exception e){Log.w("insertFail", "insert failed: " + e.toString());}


}
  public Item getEntry(long _rowIndex) {
// TODO: Return a cursor to a row from the database and
// use the values to populate an instance of MyObject
  SQLiteDatabase db = dbHelper.getReadableDatabase();

  Cursor cursor = db.query(false, DATABASE_TABLE, new String[] { KEY_ID,
          KEY_ITEM, KEY_GROUP, ITEM_CLASSIFICATION, KEY_USE, KEY_ACTION, KEY_PROPERTIES, KEY_ASSOCIATION, KEY_IMG_ID}, KEY_ID + "=?",
          new String[] { String.valueOf(_rowIndex) }, null, null, null, null);
  if (cursor != null)
      cursor.moveToFirst();

  Item item = new Item(Integer.parseInt(cursor.getString(0)),
          cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),  Integer.parseInt(cursor.getString(9)));
  // return contact
  return item;
}

我在我的日志中得到了这些——对字段槽0,9的错误请求。numRows=1,numColumns=9

和----java.lang.IllegalStateException:从第0行第9列获取字段槽失败

所以您想要获取所有列,所以我认为更清晰的方法是使用
rawQuery
方法。 您可以这样做:

SELECT_QUERY = "Select * from " + DATABASE_TABLE + " where " + KEY_ID + " = ?";
Cursor c = db.rawQuery(SELECT_QUERY, new String[] {String.valueOf(_rowIndex)});
if (c.getCount() > 0 && c.moveToFirst()) {
   Item item = new Item(Integer.parseInt(cursor.getString(0)),
      cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8));
}
注意:但是,你不是指什么问题,有什么错误吗

更新:

您的问题最有可能是:

Integer.parseInt(cursor.getString(9))
列的编号从0开始,而不是从1开始,因此:

  • \u id-0
  • \u项目组-1
  • 分类-2
  • 使用-3
  • 行动-4
  • 属性-5
  • 关联-6
  • imgId-7
  • 属性-8

  • 所以您尝试获取不存在的列。修复它,然后它应该会工作。

    所以你想要得到所有列,所以我认为更清晰的方法是使用
    rawQuery
    方法。 您可以这样做:

    SELECT_QUERY = "Select * from " + DATABASE_TABLE + " where " + KEY_ID + " = ?";
    Cursor c = db.rawQuery(SELECT_QUERY, new String[] {String.valueOf(_rowIndex)});
    if (c.getCount() > 0 && c.moveToFirst()) {
       Item item = new Item(Integer.parseInt(cursor.getString(0)),
          cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8));
    }
    
    注意:但是,你不是指什么问题,有什么错误吗

    更新:

    您的问题最有可能是:

    Integer.parseInt(cursor.getString(9))
    
    列的编号从0开始,而不是从1开始,因此:

  • \u id-0
  • \u项目组-1
  • 分类-2
  • 使用-3
  • 行动-4
  • 属性-5
  • 关联-6
  • imgId-7
  • 属性-8

  • 所以您尝试获取不存在的列。修复它,然后它就会工作。

    \u rowIndex是一个整数,我想在查询中返回该行,但我不能将其放入字符串类型的数组中。。。字符串[]sry等待我更新它。我忘了把它放在弦上。选中update.\u id是否自动成为第一列?除非指定正确,否则它将自动分配?因此,如果您查询到的第一条数据是_id列,那么非常感谢您的帮助。我已经让查询工作,现在可以从我的数据库中获取一行以供使用。您的建议非常有用。_rowIndex是一个整数,我想在查询中返回该行,我不能将其放入字符串类型的数组中。。。字符串[]sry等待我更新它。我忘了把它放在弦上。选中update.\u id是否自动成为第一列?除非指定正确,否则它将自动分配?因此,如果您查询到的第一条数据是_id列,那么非常感谢您的帮助。我已经让查询工作,现在可以从我的数据库中获取一行以供使用。你的建议很有帮助。