我有一个数据库设置,我知道我正确插入它。但是,我很难通过查询返回任何数据。这是我的主类和数据库助手类。
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;
}
我在logCat中获取这些内容----对于字段插槽0,9的错误请求。 numRows = 1,numColumns = 9
和---- java.lang.IllegalStateException:从第0行col 9获取字段槽
答案 0 :(得分:1)
所以你想获得所有列,所以我认为更清晰的方法是使用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 开始,所以:
所以你试图获得非现有的列。修复它然后它应该工作。