package br.com.anima.minhaagenda.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
import br.com.anima.minhaagenda.modelo.Aluno;
/**
* Created by tharles on 20/09/16.
*/
public class AlunoDAO extends SQLiteOpenHelper {
public AlunoDAO(Context context) {
super(context, "Agenda", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql= ("CREATE TABLE Aluno(id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, site TEXT, nota REAL)");
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = ("DROP TABLE Aluno");
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("site",aluno.getSite());
dados.put("nota",aluno.getNota());
db.insert("Aluno",null,dados);
}
public List<Aluno> buscaAluno(){
String sql = ("SELECT * FROM Aluno;");
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery(sql,null);
List<Aluno> alunos=new ArrayList<Aluno>();
while(c.moveToNext()){
Aluno aluno = new Aluno();
aluno.setNome(c.getString(c.getColumnIndex("nome")));
aluno.setEndereco(c.getString(c.getColumnIndex("endereco")));
aluno.setSite(c.getString(c.getColumnIndex("site")));
aluno.setNota(c.getDouble(c.getColumnIndex("nota")));
alunos.add(aluno);
}
return alunos;
}
}