Classe AluoDao:
public class AlunoDAO extends SQLiteOpenHelper{
public AlunoDAO(Context context) {
super(context, "Agenda", null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE Alunos (Id INTEGER PRIMARY KEY,Nome TEXT NOT NULL,endereco TEXT,telefone TEXT,site TEXT,nota REAL);";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE Alunos";
db.execSQL(sql);
onCreate(db);
}
public void insere(Aluno aluno){
SQLiteDatabase db = getWritableDatabase();
ContentValues dados = new ContentValues();
dados.put("nome",aluno.getNome());
dados.put("endereco",aluno.getEndereco());
dados.put("telefone",aluno.getTelefone());
dados.put("site",aluno.getSite());
dados.put("nota",aluno.getNota());
db.insert("Alunos",null,dados);
}
public List<Aluno> buscaAlunos() {
String sql = "select * from Alunos ;";
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery(sql,null);
List<Aluno> alunos = new ArrayList<Aluno>();
while (c.moveToNext()) {
Aluno aluno = new Aluno();
aluno.setId(c.getLong(c.getColumnIndex("id")));
aluno.setNome(c.getString(c.getColumnIndex("nome")));
aluno.setEndereco(c.getString(c.getColumnIndex("endereco")));
aluno.setTelefone(c.getString(c.getColumnIndex("telefone")));
aluno.setSite(c.getString(c.getColumnIndex("site")));
aluno.setNota(c.getDouble(c.getColumnIndex("nota")));
alunos.add(aluno);
}
c.close();
return alunos;
}
}