Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add two new Column to Android SQLite Database?

I have one application on android market , now i want to change the database filed . I want to add two new field to particular table.Can you help me how to add two new columns to table ?

Here is my code.

 private static final String ALTER_USER_TABLE = "ALTER TABLE user_table ADD user_society text,user_street1 text;"; 

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
 {
    db.execSQL(ALTER_USER_TABLE);
 }

I got the following error after executing .

W/System.err(  717): android.database.sqlite.SQLiteException: near ",": syntax error: ALTER TABLE user_table ADD user_society text,user_street1 text;
W/System.err(  717):    at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
W/System.err(  717):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)
W/System.err(  717):    at com.kbobs.org.database.Database$DatabaseHelper.onUpgrade(Database.java:48)
W/System.err(  717):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:108)
W/System.err(  717):    at com.kbobs.org.database.Database.OpenDatabase(Database.java:54)
W/System.err(  717):    at com.kbobs.org.ui.Login$2.handleMessage(Login.java:222)
W/System.err(  717):    at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(  717):    at android.os.Looper.loop(Looper.java:123)
W/System.err(  717):    at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err(  717):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(  717):    at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err(  717):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
W/System.err(  717):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
like image 594
Chirag Avatar asked May 11 '12 06:05

Chirag


People also ask

How do I add two columns in SQLite?

SQLite does not support adding multiple columns to a table using a single statement. To add multiple columns to a table, you must execute multiple ALTER TABLE ADD COLUMN statements.

How do I select two columns in SQLite?

To select multiple columns from a table, simply separate the column names with commas! For example, this query selects two columns, name and birthdate , from the people table: SELECT name, birthdate FROM people; Sometimes, you may want to select all columns from a table.

How many columns can SQLite have?

Maximum Number Of Columns The maximum number of columns is 32767 in a table. The default setting for SQLITE_MAX_COLUMN is 2000.

How manually insert data in SQLite database in Android?

If you want to inset the data manually(fully graphical) do the following: Go to the DDMS perspective. File explorer (tab-menu) Locate your db (/data/data/com.


1 Answers

You can only add one column at a time. Split it into two ALTER TABLE statements and you should be fine.

private static final String ALTER_USER_TABLE_ADD_USER_SOCIETY = 
    "ALTER TABLE user_table ADD user_society TEXT";
private static final String ALTER_USER_TABLE_ADD_USER_STREET1 = 
    "ALTER TABLE user_table ADD user_street1 TEXT";

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
   db.execSQL(ALTER_USER_TABLE_ADD_USER_SOCIETY);
   db.execSQL(ALTER_USER_TABLE_ADD_USER_STREET1);
}
like image 93
Jens Avatar answered Sep 23 '22 17:09

Jens