0

I am new in Android development.Please help me How can i store columns name in Sqlite database dynamically.

Create database below here:

  public void onCreate(SQLiteDatabase db) {
  // TODO Auto-generated method stub
  db.execSQL(
  "create table contacts " +
  "(id integer primary key, name text,phone text,email text, street text,place text)"
  );
}

And insert query below here:

 public boolean insertContact  (String name, String phone, String email,String street,String place)                                     
{
  SQLiteDatabase db = this.getWritableDatabase();
  ContentValues contentValues = new ContentValues();
  contentValues.put("name", name);
  contentValues.put("phone", phone);
  contentValues.put("email", email);    
  contentValues.put("street", street);
  contentValues.put("place", place);
  db.insert("contacts", null, contentValues);
  return true;
 }
5
  • you should not change the columns name dynamically..you have to change the column names once that to need of changing it..change column values dynamically is not good .To change the columns name use ALTER command Commented Sep 3, 2016 at 6:36
  • @brahmyadigopula 1.suppose i have one textview. I can store it into database. 2.Suppose i increse two textview also in my .xml file then how can i store into Sqlite database Dynamically without change insert query. Commented Sep 3, 2016 at 7:00
  • you mean if you have more columns dynamically right?then you have to estimate the columns and create the table for it ..while creating the table make columns default.and insert the values at your desired columns Commented Sep 3, 2016 at 7:09
  • @brahmyadigopula yes you are right.Is it possible? Commented Sep 3, 2016 at 7:51
  • yes bro..of course.it is possible.. Commented Sep 3, 2016 at 9:18

2 Answers 2

1

you can use ALTER TABLE function on your onUpgrade() method, like this :

 @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// If you need to add a column
if (newVersion > oldVersion) {
    db.execSQL("ALTER TABLE foo ADD COLUMN new_column INTEGER DEFAULT 0");
}
}

Obviously, the SQLite will differ depending on the column definition

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

Comments

0

You do not need to provide a value for every column in your table to insert a new row. You can just change the code in your method to:

public long insertContact(ContentValues contentValues) {
    return db.insert("contacts", null, contentValues);
}

Let the calling function decide which values it wants to insert, which can be all, some or none of the available columns. Any values not specified will be null in the table. For example if calling function does:

EditText name, email;

// ...

ContentValues values = new ContentValues();
values.put("name", name.getText().toString());
values.put("email", email.getText().toString());
insertContact(values);

will insert a new row in the database with "name" and "email" filled in and the rest of the columns null.

Comments

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.