dimanche 21 février 2016

Getting -1 from cursor.getint()

I may Android Database app i am getting -1 from

  int id = cursor.getInt(cursor.getColumnIndex(CONTACTS_COLUMN_ID));

though my database has a column with the same name( i have checked it by pulling database). what is the problem?

code(problem arises from find method)

public class DatabaseHelper extends SQLiteOpenHelper {
public static String databaseName;
public static final String CONTACTS_TABLE_NAME = "contacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_NAME = "name";
Context context1;

DatabaseHelper(Context context,String databaseName)
{
    super(context,databaseName,null,1);
    this.databaseName=databaseName;
    context1=context;


}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table contacts" + "(" + CONTACTS_COLUMN_ID + " integer primary key,name text)");

}

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

}
public boolean insert(String name)
{
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put("name", name);
    db.insert("contacts", null, contentValues);
    return true;

}
public Cursor getData(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from contacts where id="+id+"", null );
    return res;
}
public boolean update (Integer id, String name)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("name", name);
    db.update("contacts", contentValues, "id = ? ", new String[]{Integer.toString(id)});
    return true;
}
public  int find(String username)
{
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cursor=db.query("contacts", new String[]{CONTACTS_COLUMN_ID}, CONTACTS_COLUMN_NAME+" = ?", new String[]{username}, null, null, null);
    cursor.moveToFirst();

    if((cursor!=null)&&(cursor.getCount()!=0)) {
        int id = cursor.getInt(cursor.getColumnIndex(CONTACTS_COLUMN_ID));
        return id;
    }
    else
    {
        return -2;
    }

}
public Integer delete (Integer id)
{
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete("contacts",
            "id = ? ",
            new String[] { Integer.toString(id) });
}
public String[] get_all_entry()
{
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cursor=db.query("contacts",null,null,null,null,null,null);


    cursor.moveToFirst();
    if((cursor!=null)&&(cursor.getCount()!=0))
    {
        String []name=new String[cursor.getCount()];

        for(int i=0;i<cursor.getCount();i++)
        {
            name[i]=(cursor.getString(cursor.getColumnIndex(CONTACTS_COLUMN_NAME)));
            cursor.moveToNext();
        }

        return name;
    }
    else
    {
        return null;
    }

}
}

Aucun commentaire:

Enregistrer un commentaire