-1

i have five tables in my database i want to add two more colomns to only one table how can i please help me thanks in advance. this is my tables

                   sqLiteDatabase.execSQL(CREATE_ProductSales_TABLE);
                 sqLiteDatabase.execSQL(CREATE_UNIT_TABLE);
                  sqLiteDatabase.execSQL(CREATE_STOCK_TABLE);
                 sqLiteDatabase.execSQL(CREATE_OrderSales_TABLE);(i want to add two coloms to this table)
                sqLiteDatabase.execSQL(CREATE_Prod_TABLE);
                 sqLiteDatabase.execSQL(CREATE_CAT_TABLE);

how to excute onupgrade method what should i write for updating table and non updating table in android

sai android
  • 139
  • 1
  • 12
  • What is `CREATE_OrderSales_TABLE`? I [presume](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#execSQL(java.lang.String)) it is a string, but what is its value? How do you set its value? – HoneyBadger Jun 01 '18 at 11:56

3 Answers3

0

After the table is created you can use an alter statement to add columns such as

ALTER TABLE {OrderSales_Table} ADD COLUMN ColumnName {Integer};
Ian-Fogelman
  • 1,595
  • 1
  • 9
  • 15
0

Use a SQLiteOpenHelper class which I use to create my tables.

take a look at my class

public class DBHelper extends SQLiteOpenHelper {

// Database Information
public static final String DB_NAME = "ROADTRIP_DB";

// database version
static final int DB_VERSION = 1;

//<editor-fold desc = "TABLE NAMES">
public static final String DETAILS = "details";
//</editor-fold>

//<editor-fold desc="TABLE DETAILS">
public static final String ID = "_id";
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String MOBILE = "mobile";
public static final String DATETIMESTAMP = "datetimestamp";
public static final String STATUS = "status";
//</editor-fold>


//<editor-fold desc="CREATE_ROADTRIP_TABLE">
private static final String CREATE_ROADTRIP_TABLE = "CREATE TABLE IF NOT EXISTS " + DETAILS + "("
        + ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + NAME + " VARCHAR,"
        + EMAIL + " VARCHAR,"
        + MOBILE + " VARCHAR,"
        + DATETIMESTAMP + " DATETIME,"
        + STATUS + " INTEGER)";
//</editor-fold>


public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_ROADTRIP_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    onCreate(db);
    }
}

I also use a another class to add a command on my database

public class DBManager {

private DBHelper DBHelper;

private Context context;

private SQLiteDatabase database;

public DBManager(Context c) {
    context = c;
}

public DBManager open() throws SQLException {
    DBHelper = new DBHelper(context);
    database = DBHelper.getWritableDatabase();
    return this;
}

public void close() {
    DBHelper.close();
}

//inserting data to database
//<editor-fold desc = "insertToDetails">
public void insertToDetails(String name,
                                  String email,
                                  String mobile,
                                  String datetimestamp,
                                  String status) {

    ContentValues contentValue = new ContentValues();
    contentValue.put(DBHelper.NAME, name);
    contentValue.put(DBHelper.EMAIL, email);
    contentValue.put(DBHelper.MOBILE, mobile);
    contentValue.put(DBHelper.DATETIMESTAMP, datetimestamp);
    contentValue.put(DBHelper.STATUS, status);

    database.insert(DETAILS, null, contentValue);
}
//</editor-fold>

//for getting the data from database fuction
public Cursor fetchData() {
    String[] columns = new String[]{
            DBHelper.ID,
            DBHelper.NAME,
            DBHelper.EMAIL,
            DBHelper.MOBILE
    };
    Cursor cursor = database.query(DETAILS, columns, null, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
    }
    return cursor;
}

}

And when i want to add a data to my database. i just call

   dbManager.insertToDetails("column1","column2"...);

take a time to read this. it really make your life with database easier

EDIT

before you can use this, make sure you have to declare this:

SQLiteDatabase sqldb;
DBHelper dbHelper;
DBManager dbManager;

and call this on your onCreate (for extends AppCompactActivity) or onCreateView (for Fragment) whenever you want to use your database to prevent Database Leaks. Hope this helps

dbHelper = new DBHelper(getContext());
sqldb = dbHelper.getReadableDatabase();
dbManager = new DBManager(getActivity());
dbManager.open();
FroyoDevourer
  • 129
  • 11
0

You can use alter statement to add new column like as

sqLiteDatabase.execSQL("ALTER TABLE Tablename ADD COLUMN Newcolumn VARCHAR DEFAULT 0");

F5 Buddy
  • 474
  • 4
  • 4