在Java Sqlite DB中获取列的总和并存储在另一列中

时间:2013-12-18 19:00:48

标签: java sqlite

使用Eclipse IDE在这里初创app开发人员。我想要做的就是获取SQLite数据库中数字列的总和,然后将该总和存储在数据库的另一列中。我一直在寻找已被问过的类似问题,但我没有看到一个足够接近我的问题来实现它。我们将非常感谢您提供的任何帮助。这是我到目前为止所做的:

    public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_TITLE = "title";
static final String KEY_GENRE = "genre";
static final String KEY_DATE = "date";
static final String KEY_PRICE = "price";
static final String KEY_TOTAL = "total";
static final String KEY_WHEREBOUGHT = "wherebought";
static final String TAG = "DBAdapter";

static final String DATABASE_NAME = "gamesDB";
static final String DATABASE_TABLE = "purchases";
static final int DATABASE_VERSION = 1;

static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS purchases(_id integer primary key autoincrement, "
        + "title text not null, genre text not null, date text not null, price double not null, total double, wherebought text not null);";

final Context context;

DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBAdapter(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(DATABASE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS purchases");
        onCreate(db);
    }
}





// ---opens the database---
public DBAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

// ---closes the database---
public void close() {
    DBHelper.close();
}

// ---insert a record into the database---
public long insertRecord(String title, String genre, String date,
        double price, String wherebought) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_GENRE, genre);
    initialValues.put(KEY_DATE, date);
    initialValues.put(KEY_PRICE, price);
    initialValues.put(KEY_WHEREBOUGHT, wherebought);
    //initialValues.put(KEY_TOTAL, total);
    return db.insert(DATABASE_TABLE, null, initialValues);}


    public void calculateTotal() {

         float columntotal = 0;
         Cursor cursor1 = db.rawQuery(
             "SELECT SUM(price) FROM DATABASE_TABLE", null);
               if(cursor1.moveToFirst()) {
                columntotal = cursor1.getFloat(0);
             }
           cursor1.close();


                      db.rawQuery("INSERT INTO (DATABASE_TABLE) VALUE(columntotal)", null);}






//delete all records

public void deleteAll() {
    context.deleteDatabase (DATABASE_NAME) ;

}




// ---deletes a particular record---
public boolean deleteRecord(long rowId) {
    return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

// ---retrieves all the records---
public Cursor getAllRecords() {
    return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
            KEY_GENRE, KEY_DATE, KEY_PRICE, KEY_WHEREBOUGHT }, null, null,
            null, null, null);
}

// ---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException {
    Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
            KEY_ROWID, KEY_TITLE, KEY_GENRE, KEY_DATE, KEY_PRICE,
            KEY_WHEREBOUGHT }, KEY_ROWID + "=" + rowId, null, null, null,
            null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

// ---updates a record---
public boolean updateRecord(long rowId, String title, String genre,
        String date, double price, String wherebought) {
    ContentValues args = new ContentValues();
    args.put(KEY_TITLE, title);
    args.put(KEY_GENRE, genre);
    args.put(KEY_DATE, date);
    args.put(KEY_PRICE, price);
    args.put(KEY_WHEREBOUGHT, wherebought);
    return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}

}

所以现在代码看起来像这样。而且它告诉我列columntotal不存在这样的列,但它不应该是一个列,它是运行总计..​​.我是否在INSERT语句中的错误位置有columntotal?

 public float calculateTotal() {

     float columntotal = 0;
     Cursor cursor1 = db.rawQuery(
         "SELECT SUM(price) FROM purchases", null);
           if(cursor1.moveToFirst()) {
            columntotal = cursor1.getFloat(0);
         }
       cursor1.close();

       return columntotal;}

  public void insertTotal() {
    db.rawQuery("INSERT INTO purchases(total) VALUES(columntotal)", null);

0 个答案:

没有答案