8
respostas

Erro pós aula "Inserindo no banco"

Final da aula "Inserindo no banco" o aplicativo parou de funcionar e está dando esse erro.

12-07 13:54:47.431 7906-7906/br.com.alura.agenda E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: br.com.alura.agenda, PID: 7906
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.alura.agenda/br.com.alura.agenda.ListaAlunosActivity}: android.database.sqlite.SQLiteException: near ";": syntax error (code 1): , while compiling: CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, telefone TEXT, site TEXT, nota REAL;
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                    Caused by: android.database.sqlite.SQLiteException: near ";": syntax error (code 1): , while compiling: CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, telefone TEXT, site TEXT, nota REAL;
                                                                       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                       at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                       at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
                                                                       at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                                       at br.com.alura.agenda.dao.AlunoDAO.onCreate(AlunoDAO.java:27)
                                                                       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                       at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
                                                                       at br.com.alura.agenda.dao.AlunoDAO.buscaAlunos(AlunoDAO.java:52)
                                                                       at br.com.alura.agenda.ListaAlunosActivity.onCreate(ListaAlunosActivity.java:24)
                                                                       at android.app.Activity.performCreate(Activity.java:6237)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:148) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
8 respostas

Pareçe que tem um ";" (ponto e virgula) na sua query:

Caused by: android.database.sqlite.SQLiteException: near ";": syntax error (code 1): , while compiling: CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, telefone TEXT, site TEXT, nota REAL;

Tenta retirar esse ponto-e-virgula.

Não resolveu :/

Assim está a classe

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 IF EXISTS 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 buscaAlunos() { String sql = "SELECT * FROM Alunos;"; SQLiteDatabase db = getReadableDatabase(); Cursor c = db.rawQuery(sql, null);

List alunos = new ArrayList(); 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;

} }

insira seu código aqui
FATAL EXCEPTION: main
                                                                   Process: br.com.alura.agenda, PID: 8436
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.alura.agenda/br.com.alura.agenda.ListaAlunosActivity}: android.database.sqlite.SQLiteException: near "REAL": syntax error (code 1): , while compiling: CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, telefone TEXT, site TEXT, nota REAL
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                    Caused by: android.database.sqlite.SQLiteException: near "REAL": syntax error (code 1): , while compiling: CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, telefone TEXT, site TEXT, nota REAL
                                                                       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                       at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                       at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
                                                                       at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                                       at br.com.alura.agenda.dao.AlunoDAO.onCreate(AlunoDAO.java:27)
                                                                       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                       at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
                                                                       at br.com.alura.agenda.dao.AlunoDAO.buscaAlunos(AlunoDAO.java:51)
                                                                       at br.com.alura.agenda.ListaAlunosActivity.onCreate(ListaAlunosActivity.java:24)
                                                                       at android.app.Activity.performCreate(Activity.java:6237)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:148) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Mas agora e exception mudou,

parece que você tem um outro erro de sintaxe:

Caused by: android.database.sqlite.SQLiteException: near "REAL": syntax error (code 1): , while compiling: CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, telefone TEXT, site TEXT, nota REAL

Não está faltando fechar o parenteses nessa query?

Não, ta tudo certinho, o erro aponta essa linha db.execSQL(sql);

Mas o metodo está todo certo @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); }

12-07 15:41:10.387 8746-8746/br.com.alura.agenda E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: br.com.alura.agenda, PID: 8746
                                                                   java.lang.RuntimeException: Unable to resume activity {br.com.alura.agenda/br.com.alura.agenda.ListaAlunosActivity}: android.database.sqlite.SQLiteException: near "REAL": syntax error (code 1): , while compiling: CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, telefone TEXT, site TEXT, nota REAL
                                                                       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
                                                                       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
                                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:148)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                    Caused by: android.database.sqlite.SQLiteException: near "REAL": syntax error (code 1): , while compiling: CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, telefone TEXT, site TEXT, nota REAL
                                                                       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                       at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                       at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
                                                                       at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                                       at br.com.alura.agenda.dao.AlunoDAO.onCreate(AlunoDAO.java:27)
                                                                       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                       at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
                                                                       at br.com.alura.agenda.dao.AlunoDAO.buscaAlunos(AlunoDAO.java:51)
                                                                       at br.com.alura.agenda.ListaAlunosActivity.carregaLista(ListaAlunosActivity.java:45)
                                                                       at br.com.alura.agenda.ListaAlunosActivity.onResume(ListaAlunosActivity.java:56)
                                                                       at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
                                                                       at android.app.Activity.performResume(Activity.java:6312)
                                                                       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
                                                                       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134) 
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
                                                                       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:148) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Olá Caio,

A exception está bem precisa nesse caso, felizmente :)

Da uma olhada na sua query:

String sql = "CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, telefone TEXT, site TEXT, nota REAL"

Ela deveria ser assim:

String sql = "CREATE TABLE Alunos (id INTEGER PRIMARY KEY, nome TEXT NOT NULL, endereco TEXT, telefone TEXT, site TEXT, nota REAL)"

Você abriu o parênteses depois do CREATE TABLE Alunos e depois não fechou, por isso disparou a exception de syntaxe incorreta.

Opa, agora sim!

Resolvido,

Obrigado!

Por favor, marque o tópico como solucionado :)