为android数据库添加值时出现异常(SQLI)

时间:2012-09-30 20:42:26

标签: android

我需要帮助我的Android简单应用程序。我努力解决它,但它无用。 当我使用additem()方法时,我得到了一个例外:

android.database.cursorindexoutofboundsexception index 0 requested with a size of 0

数据库代码

public class DBAdapter extends SQLiteOpenHelper 
{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Items_Manger";
private static final String Table = "Items";
private static final String creat_Table=
"CREATE TABLE if not exists Items(id  integer primary key autoincrement,name   text,price INTEGER)";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_price = "price";

public DBAdapter(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(creat_Table);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + Table);


    onCreate(db);
}



void additem(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_ID, contact.get_id());
    values.put(KEY_NAME, contact.get_name()); 
    values.put(KEY_price, contact.get_price());

    db.insert(Table, null, values);      
    db.close(); 
}


 Contact getitem(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(Table, new String[] { KEY_ID,
            KEY_NAME, KEY_price }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();
    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
            Integer.parseInt(cursor.getString(2)));
    // return contact
    return contact;
}


public List<Contact> getallitems() {
    List<Contact> contactList = new ArrayList<Contact>();

    String selectQuery = "SELECT  * FROM " + Table;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);


    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.set_id(Integer.parseInt(cursor.getString(0)));
            contact.set_name(cursor.getString(1));
            contact.set_price(Integer.parseInt(cursor.getString(2)));
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }


    return contactList;
}

//**************************************************************
public int updateitem(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.get_name());
    values.put(KEY_price, contact.get_price());


    return db.update(Table, values, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.get_id()) });
}

//**************************************************************
public void deleteitem(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(Table, KEY_ID + " = ",new String[] { String.valueOf(contact.get_id()) });           
    db.close();
}

按钮:

    insert.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            int idd= Integer.parseInt(eid.getText().toString());
            String namee= ename.getText().toString();
            int pricee = Integer.parseInt(eprice.getText().toString());
            Contact c = new Contact();
            c=db.getitem(idd);
            db.additem(new Contact(idd,namee,pricee));  


        }
    });

联系人课程

public class Contact {
int id;
int price;
String name;

public Contact(){}

public Contact(int _id, String _name, int _price){
this.id = _id;
this.name = _name;
this.price=_price;
}
public Contact(String _name, int _price){
this.name = _name;
this.price=_price;
}
public Contact(int _id){
this.id=_id;
}

public int get_id(){return this.id;}
public int get_price(){return this.price;}
public String get_name(){return this.name;}

public void set_id(int _id){this.id=_id;}
public void set_price(int _price){this.price=_price;}
public void set_name(String _name){this.name=_name;}

}

1 个答案:

答案 0 :(得分:1)

该异常意味着您正在尝试读取空Cursor(“索引0,大小为0”)。

问题是SQliteDatabase#query()将始终返回Cursor,Cursor可能为空但它不会是null。因此,当联系人不存在时,让我们更改getItem()以返回空值:

Contact getitem(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(Table, new String[] { KEY_ID,
            KEY_NAME, KEY_price }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);

    Contact contact = null;
    if (cursor.moveToFirst()) 
        contact = new Contact(Integer.parseInt(cursor.getString(0)), cursor.getString(1), Integer.parseInt(cursor.getString(2)));

    // return contact
    return contact;
}

现在更新Button的OnClickListener:

Contact c = db.getitem(idd);
if(c == null)
    db.additem(new Contact(idd,namee,pricee));