执行getContact()
方法
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow
我是SQLite的新手,请帮助我。
这是ny代码:
public class DatabaseHandler extends SQLiteOpenHelper
{
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
// Contacts Table Columns names
final String KEY_EXP = "EXP";
final String ID = "phone";
final String KEY_NAME = "name";
final String KEY_LIMIT = "limit";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + ID + "TEXT," + KEY_LIMIT + " TEXT,"+ KEY_NAME + " TEXT PRIMARY KEY," + KEY_EXP + " TEXT);";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_EXP, contact.getEXP());
//values.put(ID, contact.getID());
values.put( KEY_LIMIT,contact.getLimit() );
// Contact Name
//values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
}
访问getContact()时出错:
Contact getContact(String id1) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] {KEY_EXP,KEY_LIMIT}, KEY_NAME + "=?",
new String[] { id1 }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact();
contact.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
contact.setEXP(cursor.getString(cursor.getColumnIndex(KEY_EXP)));
contact.setLimit(cursor.getString(cursor.getColumnIndex(KEY_LIMIT)));
db.close();
return contact;
}
public String[] getSome(String id1)
{
SQLiteDatabase db = this.getReadableDatabase();
int x;
String s1[]=new String[17];
for(int y=0;y<17;y++)
{ x = Integer.parseInt(id1);
x = x+y;
String s = Integer.toString(x);
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_NAME,
ID }, ID + "=?",
new String[] { s }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst() ;
s1[y] =cursor.getString(0).toString();
cursor.close();
}
db.close();
return s1;
}