2012-09-30 44 views
0

我需要我的android簡單應用程序的幫助。我試圖解決這個問題,但沒用。 當我使用additem()方法我有一個例外:向android數據庫添加值時出現異常(SQLI)

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)); 


     } 
    }); 

Contact類

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大小爲0」)。

的問題是,SQliteDatabase#查詢()總是會返回一個光標,光標可能是但它不會是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; 
} 

現在更新按鈕的OnClickListener:

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

我不知道該怎麼感謝你老兄 我很欣賞你努力:) –

+0

沒有問題,請點擊勾選,如果這解決了你的問題。 – Sam

相關問題