我試圖讓列「_id」的數據(這是第一列),名和密碼欄(共3列)的指數第一列(_id)= -1
但我得到這個錯誤消息
無法讀取行0,列-1從CursorWindow ...
所以我運行一個測試,以獲得使用getColumnCount(),但它的列數返回
後,我開始把所有列的索引,我發現「_id」列返回看重(-1),這就是爲什麼我得到這個錯誤消息
我看着我的查詢語句但它沒有任何問題。
有人能告訴我我的代碼有什麼問題嗎?
public class SQLiteAdapter {
SQLiteHelper helper;
public SQLiteAdapter(Context context) {
helper = new SQLiteHelper(context);
}
public String getAllData() {
SQLiteDatabase db = helper.getWritableDatabase();
// select _id, name, password from ahmadssbdb
String[] columns = { SQLiteHelper.COL_NAME, SQLiteHelper.COL_PASSWORD };
Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, columns, null, null,
null, null, null);
StringBuffer strBuffer = new StringBuffer();
int indexUID = cursor.getColumnIndex(SQLiteHelper.COL_ID);
int indexColName = cursor.getColumnIndex(SQLiteHelper.COL_NAME);
int indexColPassword = cursor.getColumnIndex(SQLiteHelper.COL_PASSWORD);
String uid = " " + indexUID + " " + indexColName + " " + indexColPassword;
return uid;
/*************************************
* Retrieve data from each column
*************************************/
/*
* while(cursor.moveToNext()){
*
* long uid = cursor.getInt(indexUID);
* String name = cursor.getString(indexColName);
* String password = cursor.getString(indexColPassword);
* strBuffer.append(uid + " " + name + " " + password + "\n"); }
*
* return strBuffer.toString();
*/
}
static class SQLiteHelper extends SQLiteOpenHelper {
private Context context;
// Defined Database Schema
private static final String DATABASE_NAME = "dbname";
private static final String TABLE_NAME = "db_table";
private static final int DATABASE_VERSION = 4;
private static final String COL_ID= "_id";
private static final String COL_NAME = "name";
private static final String COL_PASSWORD = "password";
// List of Queries
private static final String QUERY_CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_NAME + " VARCHAR(250), "
+ COL_PASSWORD + " VARCHAR(250)" +
");";
private static final String QUERY_DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Message.message(context, "constructer was called");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
// CREATE TABLE ahamadssb_table (_id INTEGER PRIMARY KEY
// AUTOINCREMENT, name VARCHAR(255));
db.execSQL(QUERY_CREATE_TABLE);
Message.message(context, "onCreate was called");
} catch (SQLException e) {
Message.message(context, "" + e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
db.execSQL(QUERY_DROP_TABLE);
onCreate(db);
Message.message(context, "onUpgrade was called");
} catch (SQLException e) {
Message.message(context, "" + e);
}
}
}
}
嘗試重新安裝後completley應用程序卸載一次.. – Lal 2014-12-06 17:46:11