Data is not entered in sqlite

Asked

Viewed 66 times

1

Guys I’m starting now on Android, I went to do a test of entering data in the database but the data are not entered, I may be mistaken at some point, follows below the code:

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_alunos);

Class responsible for introducing students to the list:

    AlunoDAO dao = new AlunoDAO(this);
    List<Aluno> alunos =  dao.buscaAlunos();
    dao.close();

    ListView listaAlunos = ( ListView) findViewById(R.id.lista_alunos);


    ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this,android.R.layout.simple_list_item_1,alunos);
    listaAlunos.setAdapter(adapter);

    Button novoAluno = (Button) findViewById(R.id.novo_aluno);

Class responsible for entering and fetching students at the bank

  public AlunoDAO(Context context) {
    super(context, "Agenda",null, 1);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String sql = "CREATE TABLE Alunos (_id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, site TEXT, nota REAL);";

    sqLiteDatabase.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    String sql = "DROP TABLE IF EXISTS Alunos";
    sqLiteDatabase.execSQL(sql);
    onCreate(sqLiteDatabase);
}

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();//getReadableDatabase
    Cursor c = db.rawQuery(sql,null);

    List<Aluno> alunos = new ArrayList<Aluno>();
    while(c.moveToNext()){
        Aluno a = new Aluno();
        a.setId(c.getLong(c.getColumnIndex("id")));
        a.setNome(c.getString(c.getColumnIndex("nome")));
        a.setEndereco(c.getString(c.getColumnIndex("endereco")));
        a.setTelefone(c.getString(c.getColumnIndex("telefone")));
        a.setSite(c.getString(c.getColumnIndex("site")));
        a.setNota(c.getDouble(c.getColumnIndex("nota")));
        alunos.add(a);
    }
    c.close();
    return alunos;
}

No error is pointed out, using debug always visualize that the array list returned by the students search is empty, the db.insert("Alunos", null, dados); I see by the debug that the filled data arrives.

I would like you to shed some light on this problem. Thanks in advance for your cooperation.

1 answer

0

Failed you point the cursor reference to the beginning using the c.moveToFirst(). Make the appropriate modification as shown in the code below and see if it works. Any doubt, comments there!

    public List<Aluno> buscaAlunos() {
        String sql = "SELECT * FROM Alunos;";
        SQLiteDatabase db = getReadableDatabase();//getReadableDatabase
        Cursor c = db.rawQuery(sql,null);
        c.moveToFirst(); // essa alteração deve ser feita

        List<Aluno> alunos = new ArrayList<Aluno>();
        while(c.moveToNext()){
            Aluno a = new Aluno();
            a.setId(c.getLong(c.getColumnIndex("id")));
            a.setNome(c.getString(c.getColumnIndex("nome")));
            a.setEndereco(c.getString(c.getColumnIndex("endereco")));
            a.setTelefone(c.getString(c.getColumnIndex("telefone")));
            a.setSite(c.getString(c.getColumnIndex("site")));
            a.setNota(c.getDouble(c.getColumnIndex("nota")));
            alunos.add(a);
        }
        c.close();
        return alunos;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.