Syntax error when using Sqlite android "DROP TABLE IF EXISTS"

Asked

Viewed 372 times

0

inserir a descrição da imagem aqui

package com.example.wesley.bancodedadossqlite;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Wesley on 17/01/2018.
 */

public class CriaBanco extends SQLiteOpenHelper {

    private static final String NOME_BANCO = "banco.db";
    private static final String TABELA = "livros";
    private static final String ID = "_id";
    private static final String TITULO = "titulo";
    private static final String AUTOR = "autor";
    private static final String EDITORA = "editora";
    private static final int VERSAO = 1;


    @Override
    public void onCreate(SQLiteDatabase db) {

        String sql = "CREATE TABLE " + TABELA + "("
                + ID + "integer primary key autoincrement,"
                + TITULO + "text,"
                + AUTOR + "text,"
                + EDITORA + "text"
                +")";

        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

        db.execSQL("DROP TABLE IF EXISTS" + TABELA);
        onCreate(db);
    }
}
  • 2

    the EXISTS cannot be pasted to the table name. Start by separating "DROP TABLE IF EXISTS " + TABELA

  • was that msm. Thank you!

1 answer

2


When you put:

db.execSQL("DROP TABLE IF EXISTS" + TABELA)

Its result would be:

DROP TABLE IF EXISTSTABELA

So to correct, give a "space" after the EXISTS, because it is a String:

db.execSQL("DROP TABLE IF EXISTS " + TABELA)

Its result would be:

DROP TABLE IF EXISTS TABELA

Browser other questions tagged

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