Create field unique Android Studio

0

I am a beginner in android development and am trying to create a table in sqlite where the email value will be unique. However, the application is allowing you to register the same emails.

@Override
    public void onCreate(SQLiteDatabase db) {
        try {
            String sql = "CREATE TABLE usuario (" +
                "id INTEGER PRIMARY KEY NOT NULL," +
                "email TEXT NOT NULL UNIQUE," +
                "senha TEXT NOT NULL);";
            db.execSQL(sql);
            Toast.makeText(UsuarioRepository.this.contexto, "Tabela criada com sucesso", Toast.LENGTH_LONG).show();
        }catch (SQLException ex){
            Toast.makeText(UsuarioRepository.this.contexto, "Erro ao criar tabela: "+ex, Toast.LENGTH_LONG).show();
        }
    }
    
asked by anonymous 12.07.2018 / 15:33

1 answer

0

You have a nice example in this Link: Difference in use Unique and Unique Constraint index in MySQL?

In your create table follow this example:

CREATE TABLE 'phone' (
    'id' MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
    'country' DECIMAL(5,0) UNSIGNED NOT NULL,
    'area' DECIMAL(5,0) UNSIGNED NOT NULL,
    'number' DECIMAL(8,0) UNSIGNED NOT NULL,
    'extension' DECIMAL(5,0) UNSIGNED DEFAULT NULL,
    PRIMARY KEY ('id'),
    UNIQUE KEY 'ix_phone' ('country', 'area', 'number', 'extension'),
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
    
20.07.2018 / 18:24