Sqlite does not save data to table - table users have in column named email

Asked

Viewed 58 times

1

can anyone help me with the following question??

I’m creating a simple APP on android and using Sqlite, but when running the APP and entering the data in the user registration area, appears the message below


E/Sqlitelog: (1) table users have no column named email

E/Sqlitedatabase: Error inserting [email protected] nome=Bruno birth=1234 phone=1234 android.database.sqlite.Sqliteexception: table usuarios has no column named email (code 1): , while compiling: INSERT INTO usuarios(email, name, birth, telephone) (?,?,?,?)**


SQL CODE

public class Model extends SQLiteOpenHelper {

    private static final String NOME_BANCO = "banco.db";
    public static final String TABELA = "usuarios";
    public static final String ID = "_id";
    public static final String NOME = "nome";
    public static final String EMAIL = "email";
    public static final String TELEFONE = "telefone";
    public static final String NASCIMENTO = "nascimento";
    private static final int VERSAO = 1;

    //CONSTRUTOR
    public Model(Context context) {
        super(context, NOME_BANCO, null, VERSAO);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {


        String sql = " CREATE TABLE IF NOT EXISTS " +TABELA+ "("
                + ID + " integer primary key autoincrement,"
                + NOME + " text,"
                + EMAIL + " text,"
                + TELEFONE + " text,"
                + NASCIMENTO + " text "
                + ")";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL(" DROP TABLE IF EXISTS " + TABELA);
        onCreate(db);
    }

}

INSERT CODE

package com.example.crowwinter.projetoandroid;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

/**
 * Created by eduardo on 15/10/17.
 */

public class BancoController {
    private SQLiteDatabase db;
    private Model banco;

    long resultado;

    public BancoController(Context context){
        banco = new Model(context);
    }

    public String insereUsuario(String nome, String email, String telefone, String nascimento){
        ContentValues valores;

        db = banco.getWritableDatabase();

        valores = new ContentValues();

        valores.put(Model.NOME, nome);
        valores.put(Model.EMAIL, email);
        valores.put(Model.TELEFONE, telefone);
        valores.put(Model.NASCIMENTO, nascimento);

       resultado = db.insert(Model.TABELA, "null", valores);

        db.close();

        if (resultado == -1){
            return String.valueOf(resultado);
        }
        else if (resultado == resultado){
            return "Usuário já existente!";
        }

        else {
            return "Usuario cadastrado";
        }
    }
//    public  String listAll(){
//
//        db = banco.getWritableDatabase();
//
//        resultado = db.rawQuery("SELECT * FROM usuarios",null);

//    }
}

REGISTRATION SCREEN CODE

package com.example.crowwinter.projetoandroid;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Cadastrar_novo extends AppCompatActivity {

    private EditText nome;
    private EditText email;
    private EditText telefone;
    private EditText nascimento;
    private Button btnCadastrar;
    private Button btnVoltar;

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


        nome = (EditText) findViewById(R.id.txtNome);
        email = (EditText) findViewById(R.id.txtEmail);
        telefone = (EditText) findViewById(R.id.txtTelefone);
        nascimento = (EditText) findViewById(R.id.txtNascimento);
        btnCadastrar = (Button) findViewById(R.id.btn_cadastrar);
        btnVoltar = (Button) findViewById(R.id.btn_voltar);


        btnCadastrar.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                BancoController crud = new BancoController(getBaseContext());
                String resultado;

                String nomeString = nome.getText().toString();
                String emailString = email.getText().toString();
                String telefoneString = telefone.getText().toString();
                String nascimentoString = nascimento.getText().toString();

                if(nomeString.equals("")){
                    Toast.makeText(Cadastrar_novo.this,"Preencha todos os campos corretamente", Toast.LENGTH_SHORT).show();
                }
                else if (emailString.equals("")){
                    Toast.makeText(Cadastrar_novo.this,"Preencha todos os campos corretamente", Toast.LENGTH_SHORT).show();
                }
                else if (telefoneString.equals("")){
                    Toast.makeText(Cadastrar_novo.this,"Preencha todos os campos corretamente", Toast.LENGTH_SHORT).show();
                }
                else if (nascimentoString.equals("")){
                    Toast.makeText(Cadastrar_novo.this,"Preencha todos os campos corretamente", Toast.LENGTH_SHORT).show();
                }




                resultado = crud.insereUsuario(nomeString,emailString,telefoneString, nascimentoString);

               Toast.makeText(getApplicationContext(), resultado, Toast.LENGTH_LONG).show();

            }
        });


    }



    public void onClickVoltar (View v){
        Intent i = new Intent(this, Menu_Principal.class);
        startActivity(i);

    }
}
  • The first time you created already had the email column? And id column can be null? Because I saw that you did not put NOT NULL in _id

  • Probably this error happens pq vc added this column after the table has already been created and installed inside the device, uninstall the APP before running again, or changes the version of the database (if 1 puts 2, and so on) that will solve the problem

  • Or, if you don’t want to change the version, you can uninstall the app and run again

  • Leonardo Dias, change the version of the bank worked perfectly. Thank you very much!!

No answers

Browser other questions tagged

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