我无法使用此查询获取数据。它工作正常我尝试通过SQLManager直接在表上执行它。这是我的代码: 在DatabaseAdapter类中:
protected Cursor fetchCustomerInfo() {
String Query = "SELECT "
+ DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_NAME
+ ", " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_CONTACT
+ ", " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_ADDRESS
+ ", " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_EMAIL
+ ", " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.PICKUP_PRIORITY_ID
+ ", " + DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE+"."+DatabaseConstant.ITEM_TYPE
+ ", " + DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE+"."+DatabaseConstant.ITEM_TYPE_ID
+ ", " + DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE+"."+DatabaseConstant.ITEM_SERIAL
+ ", " + DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE+"."+DatabaseConstant.PICKUPITEM_ID
+ " FROM " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE
+ " INNER JOIN " + DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE
+ " ON " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_ACCODE
+ " = "+ DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_ACCODE;
Log.i("Join Customer and item", Query);
return database.rawQuery(Query, null);
}
在DatabaseController类中:
public ArrayList<CustomerInfo> fetchCustomerInfo(Context context) {
DatabaseAdapter dbAdapter = null;
ArrayList<CustomerInfo> model = new ArrayList<CustomerInfo>();
Cursor cursor = null;
try {
dbAdapter = new DatabaseAdapter(context).open();
cursor = dbAdapter.fetchCustomerInfo();
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
String name = cursor.getString(cursor
.getColumnIndex(DatabaseConstant.CUSTOMER_NAME));
String contactNumber = cursor.getString(cursor
.getColumnIndex(DatabaseConstant.CUSTOMER_CONTACT));
String customerAddress = cursor.getString(cursor
.getColumnIndex(DatabaseConstant.CUSTOMER_ADDRESS));
String customerEmail = cursor.getString(cursor
.getColumnIndex(DatabaseConstant.CUSTOMER_EMAIL));
int pickupPriorityId = cursor.getInt(cursor
.getColumnIndex(DatabaseConstant.PICKUP_PRIORITY_ID));
String itemType = cursor.getString(cursor
.getColumnIndex(DatabaseConstant.ITEM_TYPE));
String itemSerial = cursor.getString(cursor
.getColumnIndex(DatabaseConstant.ITEM_SERIAL));
int itemTypeId = cursor.getInt(cursor
.getColumnIndex(DatabaseConstant.ITEM_TYPE_ID));
int pickupItemId = cursor.getInt(cursor
.getColumnIndex(DatabaseConstant.PICKUPITEM_ID));
int customerAcc = cursor.getInt(cursor
.getColumnIndex(DatabaseConstant.CUSTOMER_ACCODE));
CustomerInfo customer = new CustomerInfo(name, contactNumber, customerAddress, customerEmail, pickupPriorityId, itemType, itemSerial, itemTypeId, pickupItemId, customerAcc);
model.add(customer);
cursor.moveToNext();
}
}
cursor.close();
dbAdapter.close();
} catch (Exception e) {
try {
cursor.close();
dbAdapter.close();
} catch (Exception e2) {
}
}
return model;
}
通过以下方式调用它:
allCustomerArraylist = dbController.fetchCustomerInfo(rootView.getContext());
显示错误:
Failed to read row 0, column -1 from a CursorWindow which has 3 rows, 9 columns.
答案 0 :(得分:0)
如果您想稍后访问其值,则还需要选择DatabaseConstant.CUSTOMER_ACCODE字段。
所以这应该有效:
protected Cursor fetchCustomerInfo() {
String Query = "SELECT "
+ DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_NAME
+ ", " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_CONTACT
+ ", " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_ADDRESS
+ ", " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_EMAIL
+ ", " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_ACCODE
+ ", " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.PICKUP_PRIORITY_ID
+ ", " + DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE+"."+DatabaseConstant.ITEM_TYPE
+ ", " + DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE+"."+DatabaseConstant.ITEM_TYPE_ID
+ ", " + DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE+"."+DatabaseConstant.ITEM_SERIAL
+ ", " + DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE+"."+DatabaseConstant.PICKUPITEM_ID
+ " FROM " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE
+ " INNER JOIN " + DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE
+ " ON " + DatabaseConstant.PICKUP_CUSTOMER_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_ACCODE
+ " = "+ DatabaseConstant.PICKUP_ITEM_DETAIL_TABLE+"."+DatabaseConstant.CUSTOMER_ACCODE;
Log.i("Join Customer and item", Query);
return database.rawQuery(Query, null);
}