我在我的應用程序中使用外部數據庫,我想從數據庫中獲取最大報告代碼(例如13-T005),並將其增加1.但是我一直在努力如何獲得最後3位數字,因爲我使用'int',它只能得到最後一位數字。我怎樣才能得到報告代碼的最後3位數字,而沒有任何問題,或者更好的報告代碼本身?謝謝。如何在Android中自動增加數據庫中的代碼?
在我MainActivity.java:
private void getNextReportCode() {
tv_Code = (TextView) findViewById(R.id.tv_Code);
String query = "SELECT SUBSTR(MAX(ReportCode),5) AS ReportCode FROM " + Constants.TABLE_REPORT; //getting the last 3 digits from the code
Cursor cursorReportCode = databaseHandler.getLastRCode(query);
int reportCode = cursorReportCode.getInt(cursorReportCode.getColumnIndex(Constants.REPORT_CODE)) +1; //increment by 1
String newReportCode = "13-T" + reportCode;
tv_Code.setText(newReportCode);
}
DatabaseHandler.java
public Cursor getLastRCode(String query) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor != null);
cursor.moveToFirst();
db.close();
return cursor;
}
考慮讓'reportCode'一個'String',並使用'cursorReportCode.getString(...)'? – LuckyMe
@LuckyMe我實際上做了這個解決方案,但是如何通過使用String來增加它? – androidBoomer
似乎像其他人一樣打我說明:) – LuckyMe