Android工作室在插入数据时崩溃

时间:2017-08-24 07:55:53

标签: android sqlite

我在这里面临一些问题我试图插入一些数据,但它一直在崩溃。但是我找不到任何错误。这里真的有任何错误吗?需要帮助:(!如果有人帮忙,我将非常感激。 我的上一个项目看起来很好但是当我尝试新的时它有一些问题。我真的不知道为什么

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);
    }

}

0 个答案:

没有答案