problem with spacing and starting with number

Asked

Viewed 177 times

0

I have an application where there is a screen with an Edittext and a button. when I press the button grabbed the Edittext String game in a column of the Database in the table tables, and then soon afterwards I create a new table of the database with the name that the user chose.

When user uses error space when starting table. When user uses number to start error name when starting table

Another detail is that in another Activity I use the table name to show in the Bar action. pass the name of the table used by a Bundle, and in the other Activity open the bank with the past name and work with it.

public class adcionartabela extends ActionBarActivity {
//inicia variaveis
SQLiteDatabase Banco = null;
Cursor cursor;
String tabela; 
EditText escrevetabela;
Button ok;
@Override

//TODO oncreate
//começa o oncreate
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editable);


    //inicializa objetos
    escrevetabela    = (EditText) findViewById(R.id.editTexttabela);
    ok               = (Button)   findViewById(R.id.buttonok);


    abrebanco();

    //TODO botão ok
    //seta função do botão OK
    ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            insert       (escrevetabela.getText().toString());
            abrebanco2   (escrevetabela.getText().toString());
            iniciartabela(escrevetabela.getText().toString());
            fechabanco();
            Intent intent = new Intent(adcionartabela.this,gerenciar.class);
            startActivity(intent);
            adcionartabela.this.finish();   
        }


    });


}

//TODO iniciartabela
//inicia tabela no banco de dados, cria todos os IDs necessários
public void iniciartabela(String tabbanco) {
    for(int x = 0 ;x<60;x++)
    {   try{
        String sql = "INSERT INTO "+tabbanco+" (bt,bt01,bt02,bt03,bt04,bt05," +
                "bt06,bt07,bt08,bt09,bt10," +
                "bt11,bt12,bt13,bt14,bt15) " +
                "values ('','','','',''," +
                "'','','','','','','','','','','') ";
        Banco.execSQL(sql);
    }
    catch(Exception erro){};
    }

}

//TODO abrebanco2
//cria tabela nova
public void abrebanco2(String tabbanco){
    try{
        String sql = "CREATE TABLE IF NOT EXISTS "+tabbanco+" (ID INTEGER PRIMARY KEY AUTOINCREMENT" +
                ", bt TEXT,bt01 TEXT, bt02 TEXT, bt03 TEXT, bt04 TEXT, bt05 TEXT, bt06 TEXT, " +
                "bt07 TEXT, bt08 TEXT, bt09 TEXT, bt10 TEXT, bt11 TEXT, bt12 TEXT, " +
                "bt13 TEXT, bt14 TEXT, bt15 TEXT)";
        Banco.execSQL(sql);

    }
    catch(Exception erro){
        Exibirmensagem("BANCO", "erro ao criar banco: =/"+ erro.getMessage(), "ok");
    }
}

It is better to make up than to block the user, but the second is also valid. the big question is how do I handle a String to save to the database correctly, or send an alert to the user that he has put an invalid name? how you would advise me to do this optimization without damaging the functioning of the application?

  • One way to do it: when the user clicks on the create table button, validate the string entered by the user using a regex (regular expression) and, if it is not valid, do not let the table be created; instead it displays a Toast stating that the given name is invalid.

  • @Piovezan , would you have an example of how to use this regex? the eclipse is not recognizing

  • I put an example as an answer.

1 answer

2


The regular expression below forces the table name to start with a letter or underscore (_) followed by other characters that can be letters, numbers, underscores or hyphens.

if (escrevetabela.getText().matches("^[A-Za-z_][A-Za-z0-9_\-]+$") {
   // Cria tabela
} else {
   // Exibe toast informando que o nome digitado é inválido
}

If you want another name format, try to study the regular expression syntax.

Browser other questions tagged

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