Pengembang aplikasi pemula di sini menggunakan Eclipse IDE. Yang ingin saya lakukan adalah mendapatkan jumlah kolom numerik di db SQLite saya dan kemudian menyimpan jumlah itu di kolom lain di DB. Saya telah melihat pertanyaan-pertanyaan serupa yang telah diajukan, namun saya tidak melihat satu pun yang cukup dekat dengan pertanyaan saya untuk mewujudkannya. Bantuan apa pun yang Anda tawarkan akan sangat dihargai. Inilah yang saya miliki sejauh ini:
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;
}
}
Jadi sekarang kodenya terlihat seperti ini. Dan itu memberi tahu saya bahwa tidak ada kolom seperti itu untuk kolom kolomtotal, tetapi itu tidak seharusnya menjadi kolom, ini adalah total yang berjalan...apakah saya memiliki kolomtotal di lokasi yang salah dalam pernyataan INSERT?
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);