如何设置默认值0以及如何删除错误“列中的空值”?

时间:2016-08-17 12:54:51

标签: android database sqlite android-studio

    public class Database extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "MyDB.db";
    public static final String TABLE_NAME = "expenses";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_AMOUNT = "amount";
    public static final String COLUMN_DESC = "description";
    public static final String COLUMN_INC = "income";
    private HashMap hp;
    public int inc;

    public Database(Context context)
    {
        super(context, DATABASE_NAME , null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(

                "CREATE TABLE " + TABLE_NAME + " ("
                        + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                        + COLUMN_AMOUNT + " INTEGER DEFAULT 0, "
                        + COLUMN_DESC + " TEXT NULL, "
                        + COLUMN_INC + " INTEGER DEFAULT 0)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS expenses");
        onCreate(db);
    }

    private Integer doUpdateInc() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor rss =  db.rawQuery( "select income from expenses ", null );
        rss.moveToFirst();
        String inc = rss.getString(rss.getColumnIndex(Database.COLUMN_INC));
        return Integer.parseInt(inc);
    }


    public boolean insertExpense  (Integer id,Integer amount, String description)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put("amount", amount);
        contentValues.put("description", description);
        Integer inc = doUpdateInc();
        db.insert("expenses", null, contentValues);

        ContentValues conttValues = new ContentValues();
        conttValues.put("income", inc - amount);
        db.update("expenses", conttValues, "id = ? ", new String[] { Integer.toString(id) } );
        return true;
    }

我收到此错误

Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
                                                                             at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
                                                                             at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                                                                             at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
                                                                             at com.sohaib.dailyexpense.Database.doUpdateInc(Database.java:77)
                                                                             at com.sohaib.dailyexpense.Database.insertContact(Database.java:87)

我想要做的是:我在按钮上调用“insertExpense”函数, 并且我想在金额和描述中添加两个值..并且在该函数中我想获取收入值并从金额中减去并再次在收入列中更新。如果我在应用程序数据库中有0个值而不是应用程序崩溃,并且如果我通过更改某些代码来增加值,而不是我运行我的应用程序而不是顺利运行。

2 个答案:

答案 0 :(得分:1)

首先,您的Cursor没有任何行。在尝试调用任何moveTo...()方法之前,请检查get...()的布尔返回值。仅当光标指向有效行时,它才会返回true

DEFAULT中的CREATE TABLE值会影响列值,而不会影响行数。当您插入一个没有该列值的行时,将使用默认值。

答案 1 :(得分:0)

 private Integer doUpdateInc() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor rss =  db.rawQuery( "select income from expenses ", null );
        rss.moveToFirst();
        if (rss.getCount() > 0){
            String inc = rss.getString(rss.getColumnIndex(Database.COLUMN_INC));
            return Integer.parseInt(inc);
        }
        else {
            return 0;
        }
    }

通过应用此代码解决。