首先,我是目前正在学习Android的编程新手。这也是我在这里的第一个问题,如果有任何错误请原谅。问题是我被困在如何获得数据库列的总和。 这是我的代码,用于从sqlite帮助程序中检索数据。
private void GetColumnTotal() {
SQLHelper ColumnTOtal = new SQLHelper(this);
try {
ColumnTOtal.open();
int total = ColumnTOtal.Total();
ColumnTOtal.close();
RemainingAmount.setText(""+total);
} catch (SQLException e) {
e.printStackTrace();
}
}
`这是我的sqlite助手的代码。我发帖都希望你帮忙。
public class SQLHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_MONEY = "SpendedMoney";
private static final String DATABASE_NAME = "BudgetDB";
private static final String DATABASE_TABLE = "BudgetTable";
private static final int DATABASE_VERSION = 1;
private final Context ourContext;
private SQLiteDatabase ourDATABSE;
private DBHelper ourHelper;
public SQLHelper(Context context) {
ourContext = context;
}
public SQLHelper open() throws SQLException {
ourHelper = new DBHelper(ourContext);
ourDATABSE = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long createEntry(String money) {
ContentValues cv = new ContentValues();
cv.put(KEY_MONEY, money);
return ourDATABSE.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
String[] columns = new String[]{KEY_ROWID, KEY_MONEY};
Cursor c = ourDATABSE.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_MONEY);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iRow) + " :- " + c.getString(iName) + "\n";
}
return result;
}
public int Total() {
Cursor cur = ourDATABSE.rawQuery("SELECT SUM(SpendedMoney) FROM BudgetTable", null);
if (cur.moveToFirst()) {
}
return cur.getInt(0);
}
private static class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_MONEY + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
解决!!大家好。还有那个贬低我问题的人.. :)
答案 0 :(得分:0)
此错误消息与SQLite无关。在其他地方你使用0,其中预期字符串资源id。一个示例可能是您使用setText(int)
而不是TextView
使用setText(CharSequence)
显示结果。堆栈跟踪应该告诉你在哪里。