0

如何在 android中创建findAll() ?我已经尝试过了,但我无法实现;

public List findAll() { Cursor c = mDb.rawQuery("select * from " + TABLE_NAME, "", "" ); }

这是Medicament.java

public class Medicament {
    private long   id;
    private String nom;
    private String fabriquant;
    private String pharmacie;
    private String description;
    private float prix;

    public Medicament( long id, String nom, String fabriquant, String pharmacie, String description, float prix ) {
        super();
        this.id = id;
        this.nom = nom;
        this.fabriquant = fabriquant;
        this.pharmacie = pharmacie;
        this.description = description;
        this.prix = prix;
    }

    public long getId() {
        return id;
    }

    public void setId( long id ) {
        this.id = id;
    }

    public String getNom() {
        return nom;
    }

    public void setNom( String nom ) {
        this.nom = nom;
    }

    public String getFabriquant() {
        return fabriquant;
    }

    public void setFabriquant( String fabriquant ) {
        this.fabriquant = fabriquant;
    }

    public String getPharmacie() {
        return pharmacie;
    }

    public void setPharmacie( String pharmacie ) {
        this.pharmacie = pharmacie;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription( String description ) {
        this.description = description;
    }

    public float getPrix() {
        return prix;
    }

    public void setPrix( float prix ) {
        this.prix = prix;
    } }

编辑:请不要忘记退货声明。

提前致谢。

4

2 回答 2

0

这样做

Cursor c = mDb.rawQuery( "select * from " + TABLE_NAME,null);
于 2013-09-26T11:06:00.293 回答
0

在您的 SQLiteOpenHelper 中尝试类似的操作:

public List<Medicament> findAll() {
    List<Medicament> medicaments = new ArrayList<Medicament>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery( "SELECT * FROM " + TABLE_NAME,null);
    if (cursor.moveToFirst()) {
        do {
            Medicament medicament = new Medicament();

            /*fill here medicament object with values from row, 
            cursor.getString(0) = id value, cursor.getString(1) etc...
            for example medicament.setName(cursor.getString(1))*/

            medicaments.add(medicament);

        }
        while (cursor.moveToNext());
    }
    return medicaments;
}

编辑 :

请记住为单元格中的值类型正确使用方法,例如Integer值 use cursor.getInt(COLUMN_NUMBER)、 forString cursor.getString(COLUMN_NUMBER)等。

于 2013-09-26T11:35:22.683 回答