Error in getWritableDatabase()?

Asked

Viewed 174 times

1

I’m doing a job for college. My project until a few minutes ago was working normally, inserting User and Book, but out of nowhere it started to give error in getWritableDatabase() and I have no idea what’s causing it.

Connection class with the BANK:

    package com.example.giovanni.trocalivro.DAO;

        public class BancoDados extends SQLiteOpenHelper {

        private static final String NOMEBANCO = "trocalivros.db";

        public static final String TABELA1 = "usuarios";
        public static final String TABELA2 = "livros";

        //Usuario
        public static final String NOME = "nome";
        public static final String CELULAR = "celular";
        public static final String EMAIL = "email";
        public static final String RUANRO = "ruaNro";
        public static final String BAIRRO = "bairro";
        public static final String CEP = "cep";
        public static final String CIDADEESTADO = "cidadeEstado";

        //Livro
        public static final String ID = "_id";
        public static final String TITULO = "titulo";
        public static final String AUTOR = "autor";
        public static final String EDITORA = "editora";
        public static final String ANOPUBLI = "anoPublicacao";
        public static final String CONDICAO = "condicao";
        public static final String CELULARUSUARIO = "celularUsuario";

        private static final int VERSAO = 5;

        public BancoDados(Context ctx){

            super(ctx, NOMEBANCO, null, VERSAO);
            System.out.println("\n\n BANCO CRIADO \n");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql1 =  "CREATE TABLE"+TABELA1+"("
                    + CELULAR + "integer primary key autoincrement,"
                    + EMAIL + "text NOT NULL,"
                    + RUANRO + "text,"
                    + BAIRRO + "text,"
                    + CEP + "text,"
                    + CIDADEESTADO + "text"
                    +")";
            db.execSQL(sql1);

            String sql2 = "CREATE TABLE"+TABELA2+"("
                    + ID + "integer primary key autoincrement,"
                    + TITULO + "text NOT NULL,"
                    + AUTOR + "text,"
                    + EDITORA + "text,"
                    + ANOPUBLI + "integer,"
                    + CONDICAO + "text,"
                    + CELULARUSUARIO + "integer NOT NULL FOREIGN KEY ("
                    + CELULARUSUARIO + ") REFERENCES"+ TABELA1+"("+ CELULAR+")"
                    +")";
            db.execSQL(sql2);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABELA1);

            db.execSQL("DROP TABLE IF EXISTS " + TABELA2);
            onCreate(db);
        }
    }

**Activity de Cadastro**:

public class Cad_Usuario_Activity extends AppCompatActivity implements View.OnClickListener {

    private Button btnCadastrarUsuario;
    private EditText txtNome;
    private EditText txtCelular;
    private EditText txtEmail;
    private EditText txtEndereco;
    private EditText txtBairro;
    private EditText txtCep;
    private EditText txtCidadeEstado;
    private TextView linkLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cad__usuario_);

        btnCadastrarUsuario = (Button) findViewById(R.id.btnCadastrarUsuario);
        txtNome = (EditText) findViewById(R.id.txtNome);
        txtCelular = (EditText) findViewById(R.id.txtCelular);
        txtEmail = (EditText) findViewById(R.id.txtEmail);
        txtEndereco = (EditText) findViewById(R.id.txtEndereco);
        txtBairro = (EditText) findViewById(R.id.txtBairro);
        txtCep = (EditText) findViewById(R.id.txtCep);
        txtCidadeEstado = (EditText) findViewById(R.id.txtCidadeEstado);


    }

    @Override
    public void onClick(View v) {

    }

    private void limpaUsuario(){
        txtNome.setText(null);
        txtCelular.setText(null);
        txtEmail.setText(null);
        txtEndereco.setText(null);
        txtBairro.setText(null);
        txtCidadeEstado.setText(null);
        txtCep.setText(null);
        txtNome.requestFocus();
    }

    public void cadastrarUsuario(View view){
        String resultado = null;

        String nomeString = txtNome.getText().toString();
        int celularString = Integer.parseInt(txtCelular.getText().toString());
        String emailString = txtEmail.getText().toString();
        String ruaNroString = txtEndereco.getText().toString();
        String bairroString = txtBairro.getText().toString();
        String cepString = txtCep.getText().toString();
        String cidadeEstadoString = txtCidadeEstado.getText().toString();

        if (view == btnCadastrarUsuario){
            try{
                UsuarioControle usuarioControle = new UsuarioControle(getBaseContext());

                resultado = usuarioControle.insereUsuario(nomeString,celularString,
                        emailString,ruaNroString,bairroString,cepString,cidadeEstadoString);

                Toast.makeText(this, resultado, Toast.LENGTH_LONG).show();
                limpaUsuario();
            } catch (Exception ex){
                Toast.makeText(this, "Falha ao cadastrar", Toast.LENGTH_LONG).show();
            }
        }
    }
}

User Control class (Where is the error):

public class UsuarioControle {

private SQLiteDatabase db;
private BancoDados banco;

public UsuarioControle (Context ctx){
    banco = new BancoDados(ctx);
}

public String insereUsuario(String nome, int celular, String email,
                            String ruaNro, String bairro, String cep, String cidadeEstado)
{
    ContentValues valores;
    long resultado;

    db = banco.getWritableDatabase();

        valores = new ContentValues();
        valores.put(BancoDados.NOME, nome);
        valores.put(BancoDados.CELULAR, celular);
        valores.put(BancoDados.EMAIL, email);
        valores.put(BancoDados.RUANRO, ruaNro);
        valores.put(BancoDados.BAIRRO, bairro);
        valores.put(BancoDados.CEP, cep);
        valores.put(BancoDados.CIDADEESTADO, cidadeEstado);

        resultado = db.insert(banco.TABELA1, null, valores);

        //System.out.println("RESULTADO " + resultado);
        db.close();

        if (resultado == -1) {
            return ("Usuário Não Cadastrado");
        } else {
            return ("Usuário Cadastrado");
        }
}

He comes to show it:

I/System.out:  BANCO CRIADO 
D/SQLiteDatabase: beginTransaction()
E/SQLiteLog: (1) near "TABLEusuarios": syntax error
D/SQLiteDatabase: endTransaction()

But I didn’t touch the bank code, and no matter how much I mess around, he keeps giving it away.

I did the debug of the program, it stops at the getWritableDatabase, enters, gives some error and goes to exception treatment.

I don’t know if he’s not creating the bank, I tried to change the version and even the name of the bank, but it’s still the same. I’m already out of options, could help me?

  • Missing a space to follow the TABLE in the SQL table creation "users". String sql1 = "CREATE TABLE"+TABELA1+"(" change to String sql1 = "CREATE TABLE "+TABELA1+" (". This same problem (lack of a space) occurs in several locations, has to correct them all.

  • 1

    Yes, I was full of mistakes, I was doing tests and watching the console and managed to make it work. Thank you!

1 answer

0

As it is displayed in the error, the problem is in the SQL creating the tables in the database!

Missing spaces, try the shape sequinte:

   String sql1 =  "CREATE TABLE "+TABELA1+" ("
                    + CELULAR + " integer primary key autoincrement, "
                    + EMAIL + " text NOT NULL, "
                    + RUANRO + " text, "
                    + BAIRRO + " text, "
                    + CEP + "text ,"
                    + CIDADEESTADO + "text )";


  String sql2 = "CREATE TABLE "+TABELA2+" ("
                    + ID + "integer primary key autoincrement, "
                    + TITULO + "text NOT NULL, "
                    + AUTOR + "text, "
                    + EDITORA + "text, "
                    + ANOPUBLI + "integer, "
                    + CONDICAO + "text, "
                    + CELULARUSUARIO + " integer NOT NULL FOREIGN KEY ( "
                    + CELULARUSUARIO + " ) REFERENCES "+ TABELA1+" ( "+ CELULAR+" ) )";

Browser other questions tagged

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