Boa noite, estou na ultima aula, e após as alterações necessárias para recuperar os alunos no servidor, meu logcat está retornando esse erro:
05-17 17:49:30.076 10762-11439/br.com.agilles.agenda D/OkHttp: {"alunos":[{"id":"33c45606-5dbf-421e-a16a-3e2ffad54f86","nome":"Débora Cristina Preté Ragonha","endereco":"Rua Das Flores","telefone":"(19)34417894","site":"www.sitedadebora.com","nota":10.0,"desativado":0,"idCliente":0},{"id":"4e22f31a-bdd8-43ad-b6be-41f0c96bafd3","nome":"Jilles Ragonha","endereco":"Rua Joao Picirilo 274, Campos Eliseos","telefone":"19991822179","site":"www.jilles.com.br","nota":9.0,"desativado":0,"idCliente":0}],"momentoDaUltimaModificacao":"2017-05-17T17:49:12.040"}
05-17 17:49:30.076 10762-11439/br.com.agilles.agenda D/OkHttp: <-- END HTTP (492-byte body)
05-17 17:49:30.096 10762-10762/br.com.agilles.agenda E/SQLiteLog: (20) statement aborts at 5: [INSERT INTO Alunos(caminhoFoto,nome,nota,id,endereco,telefone,site) VALUES (?,?,?,?,?,?,?)] datatype mismatch
05-17 17:49:30.096 10762-10762/br.com.agilles.agenda E/SQLiteDatabase: Error inserting caminhoFoto=null nome=Débora Cristina Preté Ragonha nota=10.0 id=33c45606-5dbf-421e-a16a-3e2ffad54f86 endereco=Rua Das Flores telefone=(19)34417894 site=www.sitedadebora.com
android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
#################################################################
Error Code : 20 (SQLITE_MISMATCH)
Caused By : Data type mismatch.
(datatype mismatch (code 20))
#################################################################
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:915)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1609)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
at br.com.agilles.agenda.dao.AlunoDAO.inserirAluno(AlunoDAO.java:84)
at br.com.agilles.agenda.dao.AlunoDAO.sincroniza(AlunoDAO.java:151)
at br.com.agilles.agenda.ListaAlunosActivity$3.onResponse(ListaAlunosActivity.java:76)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
05-17 17:49:30.096 10762-10762/br.com.agilles.agenda E/SQLiteLog: (20) statement aborts at 5: [INSERT INTO Alunos(caminhoFoto,nome,nota,id,endereco,telefone,site) VALUES (?,?,?,?,?,?,?)] datatype mismatch
05-17 17:49:30.096 10762-10762/br.com.agilles.agenda E/SQLiteDatabase: Error inserting caminhoFoto=null nome=Jilles Ragonha nota=9.0 id=4e22f31a-bdd8-43ad-b6be-41f0c96bafd3 endereco=Rua Joao Picirilo 274, Campos Eliseos telefone=19991822179 site=www.jilles.com.br
android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
#################################################################
Error Code : 20 (SQLITE_MISMATCH)
Caused By : Data type mismatch.
(datatype mismatch (code 20))
#################################################################
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:915)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1609)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
at br.com.agilles.agenda.dao.AlunoDAO.inserirAluno(AlunoDAO.java:84)
at br.com.agilles.agenda.dao.AlunoDAO.sincroniza(AlunoDAO.java:151)
at br.com.agilles.agenda.ListaAlunosActivity$3.onResponse(ListaAlunosActivity.java:76)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
05-17 17:49:44.046 10762-10762/br.com.agilles.agenda V/ActivityThread: updateVisibility : ActivityRecord{a6f87dd token=android.os.BinderProxy@81ec5f7 {br.com.agilles.agenda/br.com.agilles.agenda.ListaAlunosActivity}} show : true
pelo que verifiquei está tudo certinho, mas segue o código do meu DAO
package br.com.agilles.agenda.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import br.com.agilles.agenda.modelo.Aluno;
/**
* Created by Jilles on 12/02/2017.
*/
public class AlunoDAO extends SQLiteOpenHelper {
public AlunoDAO(Context context) {
super(context, "Agenda", null, 4);
}
@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, caminhoFoto TEXT);";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "";
switch (oldVersion) {
case 1:
sql = "ALTER TABLE Alunos ADD COLUMN caminhoFoto TEXT;";
db.execSQL(sql);
case 2:
String sqlCriandoNovaTabela = "CREATE TABLE Alunos_novo " +
"(id CHAR(36) PRIMARY KEY," +
" nome TEXT NOT NULL," +
" endereco TEXT," +
" telefone TEXT, " +
"site TEXT, nota REAL, caminhoFoto TEXT);";
db.execSQL(sqlCriandoNovaTabela);
String inserindoDadosTabelaAntigaNaNova = "INSERT INTO Alunos_novo " +
"(id, nome, endereco, telefone, site, nota, caminhoFoto)" +
" SELECT id, nome, endereco, telefone, site, nota, caminhoFoto " +
"FROM Alunos";
db.execSQL(inserindoDadosTabelaAntigaNaNova);
String removeTabelaAntiga = "DROP TABLE Alunos";
db.execSQL(removeTabelaAntiga);
String renomeiaTabelaNova = "ALTER TABLE Alunos_novo " +
"RENAME TO Alunos";
db.execSQL(renomeiaTabelaNova);
case 3:
String buscaAlunos = "SELECT * FROM Alunos;";
String sqlParaAtualizarAluno = "Update Alunos SET id =? WHERE id =?";
Cursor cursor = db.rawQuery(buscaAlunos, null);
List<Aluno> listaAlunos = populaAlunos(cursor);
for (Aluno a : listaAlunos
) {
db.execSQL(sqlParaAtualizarAluno, new String[]{geraUUID(), a.getId()});
}
}
}
private String geraUUID() {
return UUID.randomUUID().toString();
}
public void inserirAluno(Aluno aluno) {
SQLiteDatabase db = getWritableDatabase();
insereIdSeNecessario(aluno);
ContentValues dados = pegaDadosAluno(aluno);
db.insert("Alunos", null, dados);
}
private void insereIdSeNecessario(Aluno aluno) {
if (aluno.getId() == null) {
aluno.setId(geraUUID());
}
}
@NonNull
private ContentValues pegaDadosAluno(Aluno aluno) {
ContentValues dados = new ContentValues();
dados.put("nome", aluno.getNome());
dados.put("endereco", aluno.getEndereco());
dados.put("site", aluno.getSite());
dados.put("telefone", aluno.getTelefone());
dados.put("nota", aluno.getNota());
dados.put("caminhoFoto", aluno.getCaminhoFoto());
dados.put("id", aluno.getId());
return dados;
}
public List<Aluno> buscaAlunos() {
String sql = "SELECT * FROM Alunos;";
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery(sql, null);
List<Aluno> alunos = populaAlunos(c);
return alunos;
}
@NonNull
private List<Aluno> populaAlunos(Cursor c) {
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.setTelefone(c.getString(c.getColumnIndex("telefone")));
aluno.setNota(c.getDouble(c.getColumnIndex("nota")));
aluno.setId(c.getString(c.getColumnIndex("id")));
aluno.setCaminhoFoto(c.getString(c.getColumnIndex("caminhoFoto")));
alunos.add(aluno);
}
return alunos;
}
public void deletar(Aluno aluno) {
SQLiteDatabase db = getWritableDatabase();
String[] params = {aluno.getId().toString()};
db.delete("Alunos", "id = ?", params);
}
public void alterar(Aluno aluno) {
SQLiteDatabase db = getWritableDatabase();
ContentValues dados = pegaDadosAluno(aluno);
String[] params = {aluno.getId().toString()};
db.update("Alunos", dados, "id = ?", params);
}
public void sincroniza(List<Aluno> listaAlunos) {
for (Aluno a : listaAlunos) {
if (existe(a)) {
alterar(a);
} else {
inserirAluno(a);
}
}
}
private boolean existe(Aluno a) {
SQLiteDatabase db = getReadableDatabase();
String pesquisaAlunoSeExiste = "SELECT id FROM Alunos WHERE id= ? LIMIT 1 ";
Cursor cursor = db.rawQuery(pesquisaAlunoSeExiste, new String[]{a.getId()});
int qtde = cursor.getCount();
return qtde >0;
}
}
ao debugar, verifiquei que está trazendo todas as informações do servidor, o que pode ser esse erro?