How do I do insert validation on android?

0

Code:

package com.example.scrumfacil;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.EditText;
import android.widget.Toast;

public class CriaBanco extends SQLiteOpenHelper {
    private static final String NOME_BANCO = "banco.db";
    private static final int VERSAO = 1;
    private Context context;

    public CriaBanco(Context context) {
        super(context, NOME_BANCO, null, VERSAO);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table if not exists CadastroUsuario("
                + "usuario varchar(20) primary key not null,"
                + "nome varchar(80)not null," + "senha varchar (15) not null ,"
                + "cpf integer (11)not null ,"
                + "Profissao varchar(35) not null,"
                + " nascimento date not null)";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void salvar(String nome1, String usuario, String senha, String cpf,
            String profissao, String nascimento) {

        validarinsersao(usuario,senha);

        String sql = "insert into CadastroUsuario(nome,usuario,senha,cpf,profissao,nascimento) values ('"
                + nome1
                + "','"
                + usuario
                + "','"
                + senha
                + "','"
                + cpf
                + "','"
                + profissao + "','" + nascimento + "')";
        getWritableDatabase().execSQL(sql);


    }

    public void validar(String usuario, String senha) {

        String sql = "select usuario,senha from CadastroUsuario where usuario ='"
                + usuario + "' and senha ='" + senha + "' ";

        Cursor c = getReadableDatabase().rawQuery(sql, null);

        if ( c.getCount()!=1){

            Toast.makeText(context, "Login efetuado com sucesso",Toast.LENGTH_LONG).show();

        }else{
            Toast.makeText(context, "Usuario ou senha incorreta", Toast.LENGTH_LONG).show();        
        }
    }

        public void validarinsersao(String usuario, String senha) {
            String sql = "select usuario,senha from CadastroUsuario where usuario ='"
                    + usuario + "' and senha ='" + senha + "' ";

            Cursor c = getReadableDatabase().rawQuery(sql, null);

            if ( c.getCount()==1){

                Toast.makeText(context, "Usuario ou senha ja existe",Toast.LENGTH_LONG).show();

            }           
    }


    }

Code 2:

package com.example.scrumfacil;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class CadastroActivity extends Activity {

    private Button comfirmar; 

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



        comfirmar = (Button)findViewById(R.id.btnComfirmarCadastro);

        comfirmar.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                EditText edtN=(EditText)findViewById(R.id.editNome);
                String nome1 =edtN.getText().toString();

                EditText edtU=(EditText)findViewById(R.id.editUsuario);
                String usuario =edtU.getText().toString();

                EditText edts=(EditText)findViewById(R.id.editSenha);
                String senha =edts.getText().toString();

                EditText edtc=(EditText)findViewById(R.id.editCpf);
                String cpf =edtc.getText().toString();

                EditText edtp=(EditText)findViewById(R.id.editProf);
                String profissao =edtp.getText().toString();

                EditText edtn=(EditText)findViewById(R.id.editNasc);
                String nascimento =edtn.getText().toString(); 

                if(edtN.getText().toString().trim().equals("")){
                       edtN.setError("Campo nome vazio");


                }else if (edtU.getText().toString().trim().equals("")){

                    edtU.setError("Campo usuario vazio");

                }           
                else if (edts.getText().toString().trim().equals("")){

                    edts.setError("Campo senha vazio");

                }           
                else if (edtc.getText().toString().trim().equals("")){

                    edtc.setError("Campo CPF vazio");

                }           
                else if (edtp.getText().toString().trim().equals("")){
                    edtp.setError("Campo profissao vazio");

                }           
                else if (edtn.getText().toString().trim().equals("")){

                    edtn.setError("Campo nascimento vazio");

                }else{

                CriaBanco cb = new CriaBanco(CadastroActivity.this);
                cb.salvar(nome1, usuario, senha, cpf, profissao, nascimento);     
                onPause();

                startActivity(new Intent(CadastroActivity.this,MainActivity.class));
                }
            }
        });



    }

}

Code 3:

package com.example.scrumfacil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private Button cadastro; 
    private Button login;



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

        login = (Button)findViewById(R.id.btnLogin);

        login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {


                CriaBanco validar = new CriaBanco(MainActivity.this);
                validar.validar(null,null);
                startActivity(new Intent( MainActivity.this,PlanejamentoActivity.class));
                finish();
            }
        });


        cadastro = (Button)findViewById(R.id.btnCadastrar);

        cadastro.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent( MainActivity.this,CadastroActivity.class));

            }
        });

    }


}
    
asked by anonymous 26.10.2017 / 16:07

0 answers