Error in getWritableDatabase ()? [closed]

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 is causing it.

BANK connection class:

    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 error is):

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 even shows this:

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

But I did not move in the bank code and no matter how much it moves there, it keeps giving it.

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

I do not know if he is not creating the bank, I tried to change the version and even the bank name, but it is still the same. I'm already without options, could you help me?

    
asked by anonymous 19.11.2016 / 06:19

1 answer

0

As shown in the error, the problem is in SQL that creates the tables in the database!

Missing spaces, try the following way:

   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+" ) )";
    
21.11.2016 / 12:58