0

I am facing some problem here I tried to insert some data but it keep crashing. But I cant find any error. Is there really any error here. Need help :(! I will be very appreciate if anyone helps. My last project seems fine with it but when I tried a new one it have some problem. I really don't know why

public class DBHelper extends SQLiteOpenHelper {

    //Stock Table Declaring
    public static final String TAG = DBHelper.class.getSimpleName();
    public static final String DB_NAME = "stockcode.db";
    public static final int DB_VERSION = 2;
    public static final String DB_TABLE = "StockTable" ;
    public static final String COLUMN_STOCKCOUNT_ID = "stockcount_id";
    public static final String COLUMN_ITEM_CODE = "item_code";
    public static final String COLUMN_Quantity = "quantity";

    /*create table StockTable{
        stockcount_id integer autoincrement pk (not allow null);
        stockcode integer
        quantity integer}
     */

    public static final String CREATE_TABLE_STOCK = "CREATE TABLE " + DB_TABLE + "("
            + COLUMN_STOCKCOUNT_ID + "INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_ITEM_CODE + "TEXT, "
            + COLUMN_Quantity + "TEXT)";

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

    //Execute the sql command create a new table stock!!!!
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(CREATE_TABLE_STOCK);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
        onCreate(db);
    }

    //adding new stock data
    public void addData(String item_code,String item_qty){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_ITEM_CODE,item_code);
        values.put(COLUMN_Quantity,item_qty);

        long id = db.insert(DB_TABLE,null,values);
        db.close();
        Log.d(TAG,"user inserted" + id);
    }

}
Sunil P
  • 3,698
  • 3
  • 13
  • 20

0 Answers0