Create Android Studio Unique Field

Asked

Viewed 53 times

0

I’m a beginner in android development and I’m trying to create a table in sqlite where the value of email will be unique. Only the app is allowing you to register equal 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();
        }
    }
  • 1

    If your application allows registering equal emails but your table does not allow then the error must be in your application. Another thing that may be occurring is that some space character at the beginning or end of the email may be differentiating seemingly equal emails. Remove any unnecessary spaces.

1 answer

0

There is a nice example on this Link: Difference in Use Index Unique and Unique Constraint 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;

Browser other questions tagged

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