我正在尝试为我的大学制作一个考勤应用程序。我使用 SQLite 进行数据存储(学生名单、出勤数据等),我希望将出勤数据导出为 CSV 文件。问题是,当我导出文件时,只有 SQLite 数据库的最后一个条目被写入 CSV。为了更好地理解,我附上了下面的代码。
public void exportExcelSheet() {
DatabaseHandler mDbHandler = new DatabaseHandler(mActivity);
File exportDir = new File(Environment.getExternalStorageDirectory(), "");
if (!exportDir.exists())
{
exportDir.mkdirs();
}
File file = new File(exportDir, "Report.csv");
String[] ColumnNames = {"Roll No.","Name","LA","LT","% age"};
String studentInfoQuery = "SELECT * FROM StudentList";
Cursor studentsListCursor = mDbHandler.execQuery(studentInfoQuery);
studentsListCursor.moveToFirst();
while(!studentsListCursor.isAfterLast()) {
String AttendanceListQuery = "SELECT * FROM AttendanceSheet WHERE StudRoll = '" + studentsListCursor.getPosition() + "';";
String AttendanceQuery = "SELECT * FROM AttendanceSheet WHERE StudRoll = '" + studentsListCursor.getPosition() + "' AND isPresent = 1";
int attendancePercent = 0;
Cursor totalClasses = mDbHandler.execQuery(AttendanceListQuery);
Cursor attendedClasses = mDbHandler.execQuery(AttendanceQuery);
if (totalClasses == null) {
Log.d("profile", "totalClasses null");
}
if (attendedClasses == null) {
Log.d("profile", "attendedClasses null");
}
if (totalClasses != null && attendedClasses != null) {
totalClasses.moveToFirst();
attendedClasses.moveToFirst();
try {
attendancePercent = (int) (((float) attendedClasses.getCount() / totalClasses.getCount()) * 100);
} catch (Exception e) {
attendancePercent = -1;
}
}
慕村9548890
九州编程
相关分类