problem with spacing and starting with number

0

I have an application where it has a screen with an EditText and a button. when I press the button I get the EditText String set in a Database column in the tables table, and then I create a new database table with the name that the user chose.

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

Another detail is that in another activity I use the name of the table to show in the Action Bar. I would like to change the name of the table used by a bundle, and in the other activity I open the database with the last 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 preferable to paint this than to block the user, but the second one is also valid. the big question how do I treat a String to save to the database correctly, or send an alert to the user that it has placed an invalid name? how would you advise me to do this optimization without damaging the application?

    
asked by anonymous 29.10.2014 / 19:50

1 answer

2

The regular expression below forces the table name to begin 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, you should study syntax regular expressions .

    
29.10.2014 / 20:50