亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 SQLite 數據導出到 CSV 僅輸出 CSV 文件中數據庫的最后一個條目

將 SQLite 數據導出到 CSV 僅輸出 CSV 文件中數據庫的最后一個條目

泛舟湖上清波郎朗 2023-07-13 18:13:29
我正在嘗試為我的大學制作一個考勤應用程序。我使用 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;? ? ? ? ? ? ? ? }? ? ? ? ? ? }
查看完整描述

2 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

對于其他正在尋找類似問題答案的人,根據 Dheeraj 的代碼和另一項細微更改,最終的工作代碼將是 -


 public void exportExcelSheet() throws IOException {

    DatabaseHandler mDbHandler = new DatabaseHandler(mActivity);

    File exportDir = new File(Environment.getExternalStorageDirectory(), "");

    if (!exportDir.exists()) {

        exportDir.mkdirs();

    }


    File file = new File(exportDir, "Report.csv");


    if (!file.exists()) {

        try {

            file.createNewFile();

        } catch (IOException e) {

            e.printStackTrace();

        }


    }


    String[] ColumnNames = {"Roll No.", "Name", "LA", "LT", "% age"};


    CSVWriter csvWrite = new CSVWriter(new FileWriter(file));

    csvWrite.writeNext(ColumnNames);


    String studentInfoQuery = "SELECT * FROM StudentList";

    Cursor studentsListCursor = mDbHandler.execQuery(studentInfoQuery);


    studentsListCursor.moveToFirst();


    do {

        int studentRoll = studentsListCursor.getPosition() + 1;

        String AttendanceListQuery = "SELECT * FROM AttendanceSheet WHERE StudRoll = '" + studentRoll + "';";

        String AttendanceQuery = "SELECT * FROM AttendanceSheet WHERE StudRoll = '" + studentRoll + "' 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;

            }

        }


        assert attendedClasses != null;

        assert totalClasses != null;

        String showAttendedLectures = String.valueOf(attendedClasses.getCount());

        String showTotalLectures = String.valueOf(totalClasses.getCount());

        //String showMissedLectures = String.valueOf(totalClasses.getCount() - attendedClasses.getCount());

        String AttendancePercentage = String.valueOf(attendancePercent);


        try {

            String[] arrStr = {studentsListCursor.getString(1), studentsListCursor.getString(0), showAttendedLectures, showTotalLectures, AttendancePercentage + " %"};

            csvWrite.writeNext(arrStr);


        } catch (Exception sqlException) {

            Toast.makeText(mActivity, "FAILED", Toast.LENGTH_SHORT).show();

            Log.e("MainActivity", sqlException.getMessage(), sqlException);

        }


        Toast.makeText(mActivity, "Saved", Toast.LENGTH_SHORT).show();

    }


    while (studentsListCursor.moveToNext());


    csvWrite.close();

}



查看完整回答
反對 回復 2023-07-13
?
九州編程

TA貢獻1785條經驗 獲得超4個贊

這段代碼中有一個小錯誤,即每次執行 do-while 循環時都會創建 csvWriter 對象,因此最后一個輸出 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");


    // ============== CHANGE ==============

    if (!file.exists()) {

        try {

            file.createNewFile();

        } catch (IOException e) {

            e.printStackTrace();

        }


    }


    String[] ColumnNames = {"Roll No.", "Name", "LA", "LT", "% age"};


    // ============== CHANGE ==============

    CSVWriter csvWrite = new CSVWriter(new FileWriter(file));

    csvWrite.writeNext(ColumnNames);


    String studentInfoQuery = "SELECT * FROM StudentList";

    Cursor studentsListCursor = mDbHandler.execQuery(studentInfoQuery);


    studentsListCursor.moveToFirst();


    // ============== CHANGE ==============

    do {

        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;

            }

        }


        assert attendedClasses != null;

        assert totalClasses != null;

        String showAttendedLectures = String.valueOf(attendedClasses.getCount());

        String showTotalLectures = String.valueOf(totalClasses.getCount());

        //String showMissedLectures = String.valueOf(totalClasses.getCount() - attendedClasses.getCount());

        String AttendancePercentage = String.valueOf(attendancePercent);


        try {

            String[] arrStr = {studentsListCursor.getString(1), studentsListCursor.getString(0), showAttendedLectures, showTotalLectures, AttendancePercentage + " %"};

            csvWrite.writeNext(arrStr);

            // ============== CHANGE ==============

            // studentsListCursor.moveToNext();

        } catch (Exception sqlException) {

            Toast.makeText(mActivity, "FAILED", Toast.LENGTH_SHORT).show();

            Log.e("MainActivity", sqlException.getMessage(), sqlException);

        }


        Toast.makeText(mActivity, "Saved", Toast.LENGTH_SHORT).show();

    }

    // ============== CHANGE ==============

    while (studentsListCursor.moveToNext());


    csvWrite.close();

}


查看完整回答
反對 回復 2023-07-13
  • 2 回答
  • 0 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號