我正在尝试获取列" _id"的数据。 (这是第一列),名称和密码列(共3列)
但是我收到了此错误消息
无法从CursorWindow读取第0行,第-1列......
所以我运行测试以使用getColumnCount()获取列数但返回 2
之后我开始获取所有列的索引,我发现" _id" 列返回值( - 1),并且&# 39;为什么我收到错误信息
我查看了我的查询语句,但它没有任何问题。
有人能告诉我我的代码有什么问题吗?
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);
}
}
}
}
答案 0 :(得分:3)
您的查询没有要求id列。将它添加到您的columns变量:
String[] columns = { SQLiteHelper.COL_ID, SQLiteHelper.COL_NAME, SQLiteHelper.COL_PASSWORD };
答案 1 :(得分:1)
你必须添加" _id"列到投影数组。 它在投影中不存在,因此getColumnIndex返回-1
http://developer.android.com/reference/android/database/Cursor.html#getColumnIndex(java.lang.String)