0

我正在尝试使用SQLITE. 这是我的代码

package com.example.dbex;

import java.sql.SQLOutput;
import java.sql.SQLPermission;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHandler extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION=1;
    public static final String DATABASE_NAME="contactsmanager";
    public static final String TABLE_CONTACTS="contacts";
    public static final String KEY_ID="id";
    public static final String KEY_NAME="name";
    public static final String KEY_NUMBER="number";
    public DBHandler(Context context) {
        super(context,DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String CREATE_TABLE_CONTACTS="CREATE TABLE "+TABLE_CONTACTS+"("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_NAME+" TEXT,"+KEY_NUMBER+" TEXT"+")";
        db.execSQL(CREATE_TABLE_CONTACTS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_CONTACTS);
        onCreate(db);
    }

    public void addContact(Contacts contacts)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(KEY_NAME, contacts.getname());
        values.put(KEY_NUMBER, contacts.getnumber());
        db.insert(TABLE_CONTACTS,null,values);
        db.close();
    }

    public Contacts getContact(int id)
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c=db.query(TABLE_CONTACTS, new String[]{KEY_ID,KEY_NAME,KEY_NUMBER},KEY_ID+" =?",new String[]{String.valueOf(id)}, null, null, null, null);
        if(c!=null)
        {
            c.moveToFirst();
        }
        Contacts contacts=new Contacts(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));
        return contacts;
    }

    public void deleteContact(Contacts contacts)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID+" =?", new String[]{String.valueOf(contacts.getID())});
        db.close();
    }


}

`

我在行上遇到光标越界异常

Contacts contacts=new Contacts(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));

我哪里错了?

4

3 回答 3

1

您的光标可能为空。

它可以不为空,但仍然没有结果(返回 0 行)。您可以通过将逻辑更改为:

Contact contacts = null;
if (c!=null && c.moveToFirst()) {
    contacts = new Contacts(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));
}
return contacts;
于 2013-06-28T10:00:25.823 回答
0

游标越界异常通常发生在当游标内没有那么多行时尝试访问游标位置时。打印有关游标行数的日志,如果它不为 0,则仅尝试访问更多行。

于 2013-06-28T10:02:50.973 回答
0

试试下面

    Contacts getContact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
            KEY_NAME, KEY_NUMBER }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    Contacts contacts; 
    if (cursor != null)
    {
    cursor.moveToFirst();
    contacts = new Contacts(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2));
    }
    return contacts

}
于 2013-06-28T10:04:18.713 回答