1

I've been searching for hours, and I can't seem to find the answer that I've been looking for. I want to create a table(a subject table) based on user input (User will only input table name) and inside that table, the user can add information (like students enrolled in that particular subject. Is this possible in android? I'm using eclipse. I'm really a newbie in android programming. Can you please give me some tips what to search or what should I try doing to achieve this. Please help I'm just asking for advice/tips/tutorials that might help me. Thanks in advance. Sorry for my bad English. OUTPUT

NOTE: The main problem here is I don't know how to create table from user input. Please I need advice/tips.

4
  • Do you know how to create tables in SQLite? Commented May 6, 2014 at 5:04
  • Yes. Pardon me for being a total newbie. Commented May 6, 2014 at 5:11
  • 1
    Then I don't see where do you have problems. You will take the user input text(from the EditText or whatever input method you have in mind), check the input(not all users are nice guys) and then create the table in sqlite with a execSQL() call. Commented May 6, 2014 at 5:17
  • the problem is i don't know how can I check the input to create the table. :( how can I equal user input to table name? Commented May 6, 2014 at 5:23

1 Answer 1

2

Ok, I'm still not sure where do you have problems, but I'm posting this because it's too big for a comment. In your current setup, let's assume that you have an EditText where the user will enter the new subject(like math for example). To make the new table you'll need two operations: to insert the math text in the subject table and then actually create the new math table. You'll need to do this in a transaction to make sure this operations either succeed or fail as one( http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#beginTransaction%28%29 ). First, to insert the math text in the subject table you'll use http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert%28java.lang.String,%20java.lang.String,%20android.content.ContentValues%29 . Then to create the new math table you'll do:

// get the text entered by the user, also trim it for extra spaces
String newTableName = editTextInput.getText().trim(); 
// the entered name should consist only of characters
newTableName = newTableName.replaceAll("[^a-zA-Z]", "");
// create the new table
sqliteDatabase.execSQL("CREATE TABLE " + newTableName);

The way you setup the database is far from ideal. You should avoid creating tables for each of the subject table entries(and for the students as well, I'm assuming a student will be engaged in more than one course), instead you should create a subject table, a students tables(each with a unique id) and one table that joins the two of them(also look at sqlite foreign keys).

Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thanks for the idea! Also thanks for your advise about my database setup. I will to research more about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.