Question

as I am new to android programming, I am creating a Table and naming it with the text in edittext. how can i do it. searched questions but did not get any answer. And my application needs to create many tables at run time. i am using SQLiteOpenHelper class so please help me doing it. I have created a function to create tables, and written nothing in the onCreate()/onUpgrade function.

public void createClass(String table){
    shuddhHelper = new DbHelper(shuddhContext);
    SQLiteDatabase db = shuddhHelper.getWritableDatabase();
    db.execSQL("CREATE TABLE " + table + " (" +
            STUDENT_NAME + " TEXT NOT NULL, " +
            ROLL_NO + " TEXT PRIMARY KEY, " +
            STUDENT_PHONE + " INTEGER NOT NULL, " +
            PARENT_PHONE + " INTEGER NOT NULL);"                
            );      
}

and called the function like this in a onClickListener

className = "" + spinner.getSelectedItem().toString() + " "
            + sessionStart.getText().toString() + " - "
            + sessionEnd.getText().toString();
    AMData classtable = new AMData(AddScreen.this);
    classtable.open();
    classtable.createClass(className);
    classtable.close();

     Intent myIntent = new Intent(AddScreen.this, NewStudent.class);
     myIntent.putExtra("className", className);
     startActivity(myIntent);

and open and close functions here:

public AMData open() {
    shuddhHelper = new DbHelper(shuddhContext);
    shuddhDataBase = shuddhHelper.getWritableDatabase();
    return this;
}
public void close(){
    shuddhHelper.close();
}
Was it helpful?

Solution

Your table name is wrong as it includes whitespaces.

try the following:

className = "" + spinner.getSelectedItem().toString() + "_"
        + sessionStart.getText().toString() + "-"
        + sessionEnd.getText().toString();

By the way: If you have to concenate several strings it is always better to use a StringBuilder. It uses less memory space and computing time.

String class = new StringBuilder().append(spinner.getSelectedItem().toString())
                                  .append("_")
                                  .append(sessionStart.getText().toString())
                                  .append("-")
                                  .append(sessionEnd.getText().toString())
                                  .toString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top