-1

我将数据插入到 sqlite 数据库表中,当我插入第一条记录时,它插入成功,但第二条和所有下一条记录都没有插入。这是我的数据库助手类。我在 Firefox 的 sqlite manager 中尝试了相同的查询,它们工作正常。我不知道为什么他们不在我的代码中工作。

    private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + "( " + KEY_ROWID +
        " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DESCRIPTION + " text, "+ KEY_WHY+" text , "+ KEY_ACCOUNTABILITY+
        " text , "+KEY_ALARM_TYPE+" text , "+KEY_ALARM_VOLUME+" text,  "+KEY_BG_IMAGE+" text, "+KEY_CATEGORY+"  text, "+KEY_DATE_TIME+  " datetime "+" , "+KEY_STATUS+" text );";
    public void addRecord(String description,String why, String accontability, String alarm_type,
            String alarm_vol, String bg_image, String category, Date goalDateTime,String status) {
        String query = "INSERT INTO " + DATABASE_TABLE + " VALUES(null,'"+ description + "', '" + why +  "', '" + accontability +"','" + alarm_type +
                "', '" + alarm_vol +  "', '" + bg_image +"','" +category+ "','" +  goalDateTime +"','"+status+"');";
        Log.v("--->", query);
        try{
        mDB.execSQL(query);



    } catch (Exception ex) {
        Log.v("database error", ex.toString());

    }
    }

在这里我在表中添加数据

    try {
        /*
         * dbHelper = new GoalDbAdapter(this); dbHelper.open();
         * Log.v("database ","database opened"); // dbHelper. fillData();
         */
        db = new GoalDbAdapter(this);
        db.open();
        String imagePath = pref.getValue("imagePath", "");
        String goalCateg = pref.getValue("categText", "");
        String vol = pref.getValue("vol", "");
        String alarmType=  pref.getValue("ringTonePath", "");
        db.addRecord(goal_desc, goal_why, accountability, alarmType, vol, imagePath, goalCateg, goalDate,"");
         cursor = db.getTop3Goals();
        Log.v("cursor count in add act",".."+cursor.getCount());
        cursor.close();
        db.close();
    } catch (Exception ex) {
        Log.v("database error", ex.toString());

    }

这是我获取目标的代码,我无法获取所有数据,它只返回第一个目标而不返回其他目标。所有这些方法都只返回第一个目标。

     public Cursor getGoals() {
    Cursor c_all = mDB.rawQuery("SELECT * FROM " + DATABASE_TABLE, null);
    int count= c_all.getCount();
    Log.v("cursor count in db all goals", c_all.getCount()+"..");
    c_all.moveToFirst();
    return c_all;
}

//SELECT * FROM table ORDER BY date DESC LIMIT 5

public Cursor getTop3Goals() {
    Cursor c = mDB.rawQuery("SELECT * FROM " + DATABASE_TABLE + " ORDER BY "+ KEY_DATE_TIME + " DESC LIMIT 3", null);
    c.moveToFirst();
    return c;
}

public Cursor getGoalsByDate() {
    Cursor c = mDB.rawQuery("SELECT * FROM " + DATABASE_TABLE + " ORDER BY "+ KEY_DATE_TIME + " DESC ", null);
    c.moveToFirst();
    return c;
}
4

1 回答 1

1

尝试像这样更改您的插入方法:

    public void addRecord(String description,String why, String accontability, String alarm_type,
                    String alarm_vol, String bg_image, String category, Date goalDateTime,String status){

      ContentValues initialValues = new ContentValues();

      initialValues.put(KEY_DESCRIPTION , description);
      initialValues.put(KEY_WHY, why);
      initialValues.put(KEY_ACCOUNTABILITY, accontability);
      initialValues.put(KEY_ALARM_TYPE, alarm_type);

      initialValues.put(KEY_ALARM_VOLUME, alarm_vol);
      initialValues.put(KEY_BG_IMAGE, bg_image);
      initialValues.put(KEY_CATEGORY, category);
      initialValues.put(KEY_DATE_TIME, goalDateTime);
      initialValues.put(KEY_STATUS, status);

      long rowId = mDB.insert(DATABASE_TABLE , null, initialValues);


}

rowId是新记录的行 ID 号。

编辑: 尝试下一个代码:

public Cursor getGoals() {
    Cursor cursor = mDB.query(DATABASE_TABLE, null, null, null, null, null, null);

        if ((cursor != null) && (cursor.getCount() > 0)) {
            cursor.moveToNext();

            return cursor;
        }

    return null;
}
于 2012-05-19T19:09:24.107 回答