数据未插入数据库应用程序android

时间:2018-01-11 17:18:40

标签: android sqlite

我有一个数据库应用程序。在这个应用程序中我在内部插入数据但似乎没有插入数据。我的帮助班是----

public class ThirdHelper extends SQLiteOpenHelper
{

    public ThirdHelper(Context context) {
        super(context, TABLE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        String CREATE_TABLE=  "CREATE TABLE " + TABLE_NAME + " ("
                + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + MATH+ " INTEGER NOT NULL,"
                + DSA+ " INTEGER NOT NULL,"
                + DEC+ " INTEGER NOT NULL,"
                + WT+ " INTEGER NOT NULL,"
                + DMS+" INTEGER NOT NULL);";

        db.execSQL(CREATE_TABLE);

        insertData(4,4,3,4,4);

    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }

    public void insertData(Integer math,Integer dsa,Integer dec,Integer wt,Integer dms)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(MATH,math);
        values.put(DSA,dsa);
        values.put(DEC,dec);
        values.put(WT,wt);
        values.put(DMS,dms);
        db.insert(TABLE_NAME,null,values);

        db.close();
    }

在我的活动类游标中如下 -

Cursor c = db.query(TABLE_NAME, new String[]{_ID,MATH, DSA, DEC, WT, DMS}, null, null, null, null, null);


                if (c!=null) {
                    c.moveToFirst();
                   while(!c.isAfterLast()) {
                        math = c.getInt(c.getColumnIndex(MATH));
                        dsa = c.getInt(c.getColumnIndex(DSA));
                        dec = c.getInt(c.getColumnIndex(DEC));
                        wt = c.getInt(c.getColumnIndex(WT));
                        dms = c.getInt(c.getColumnIndex(DMS));
                    }
                }

               double d = ((mathInt / 10 + 1) * math + (dsaInt / 10 + 1) * dsa + (decInt / 10 + 1) * dec + (wtInt / 10 + 1) * wt + (dmsInt / 10 + 1) * dms);

似乎getInt()返回0.这就是为什么double' d'在文本视图中显示0.为什么会发生这种情况?我在接受输入时做错了吗?

1 个答案:

答案 0 :(得分:0)

一个疯狂的猜测:执行db.query()时,您确定db对象是否有效?您可以尝试在db.close()方法中删除insertData吗?