Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取JavaSQLite数据库中的列总和并存储在另一列中_Java_Sqlite - Fatal编程技术网

获取JavaSQLite数据库中的列总和并存储在另一列中

获取JavaSQLite数据库中的列总和并存储在另一列中,java,sqlite,Java,Sqlite,使用EclipseIDE的初出茅庐的应用程序开发人员。我所要做的就是在SQLite数据库中获取一个数值列的和,然后将该和存储在数据库中的另一列中。我一直在研究被问到的类似问题,但我没有看到一个与我的问题足够接近的问题。如果您能提供任何帮助,我们将不胜感激。以下是我目前掌握的情况: public class DBAdapter { static final String KEY_ROWID = "_id"; static final String KEY_TITLE = "title";

使用EclipseIDE的初出茅庐的应用程序开发人员。我所要做的就是在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);

谢谢你,约瑟夫。关于从“我所拥有的”到“你所建议的”的语法有什么提示吗?如果你的DB支持subselect,你可以通过以下方式来完成:在表2中插入columnForTotal VALUES,从表1中选择SUMprice;如何在java中执行它是你的家庭作业:我使用了rawQuery,因为当我尝试execSQL时,我得到了一个错误,说我应该使用query来代替…我使用了错误的方法吗?你能发布完整的代码来分析谁错了吗?Thx从未使用过SQLite,但假设它只是一个sql数据库。。。。您的insert语句看起来不正确。它通常插入col1,col2的值val1,val2