2017-11-09 2 views
3

Sqliteデータベース経由でAmountという名前の列を合計するにはどうすればよいですか?私は金額を入力した金額を加算したいと思っています。挿入している間にダイプレイを作成しました。作成されたExcelシートに金額の合計が表示されました。よろしくお願いします。アドバンスでお願いします。SqliteデータベースのVeritacal Columを合計してExcelシートでエクスポートする方法

public void saveExcelFile() { 
    try { 

     dbHelper = new DBHelper(MainActivity.this); 
     final Cursor cursor = dbHelper.getuser(selectedDate); 
     //create directory if not exist 
     if (!directory.isDirectory()) { 
      directory.mkdirs(); 
     } 

     File file = new File(directory, edtDate.getText().toString() + ".xls"); 
     WorkbookSettings wbSettings = new WorkbookSettings(); 
     wbSettings.setLocale(new Locale("en", "EN")); 
     WritableWorkbook workbook; 
     workbook = Workbook.createWorkbook(file, wbSettings); 
     //Excel sheet name. 0 represents first sheet 
     WritableSheet sheet = workbook.createSheet("userList", 0); 
     // column and row 
     sheet.addCell(new Label(0, 0, "id")); 
     sheet.addCell(new Label(1, 0, "name")); 
     sheet.addCell(new Label(2, 0, "email")); 
     sheet.addCell(new Label(3, 0, "date")); 
     sheet.addCell(new Label(4, 0, "amount")); 

     if (cursor.moveToFirst()) { 
      do { 
       String id = cursor.getString(cursor.getColumnIndex("id")); 
       String name = cursor.getString(cursor.getColumnIndex("name")); 
       String email = cursor.getString(cursor.getColumnIndex("email")); 
       String date = cursor.getString(cursor.getColumnIndex("date")); 
       String amount = cursor.getString(cursor.getColumnIndex("amount")); 

       int i = cursor.getPosition() + 1; 
       sheet.addCell(new Label(0, i, id)); 
       sheet.addCell(new Label(1, i, name)); 
       sheet.addCell(new Label(2, i, email)); 
       sheet.addCell(new Label(3, i, date)); 
       sheet.addCell(new Label(4, i, amount)); 
      } while (cursor.moveToNext()); 
     } 

     //closing cursor 
     cursor.close(); 
     workbook.write(); 
     workbook.close(); 
     Toast.makeText(getApplication(), "Data Exported in a Excel Sheet", Toast.LENGTH_SHORT).show(); 
    } catch (WriteException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

答えて

0

...

  1. は、このjarファイルopencsv-1.7.jar http://www.java2s.com/Code/Jar/o/Downloadopencsv17jar.htm
  2. を追加しExportDatabaseToCSV {

    このコード

publicクラスを使用しています

Context context; 
public ExportDatabaseToCSV(Context context) { 
    this.context=context; 
} 


public void exportDataBaseIntoCSV(){ 


    CredentialDb db = new CredentialDb(context);//here CredentialDb is my database. you can create your db object. 
    File exportDir = new File(Environment.getExternalStorageDirectory(), "");   

    if (!exportDir.exists()) 
     { 
      exportDir.mkdirs(); 
     } 

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

    try 
    { 
     file.createNewFile();     
     CSVWriter csvWrite = new CSVWriter(new FileWriter(file)); 
     SQLiteDatabase sql_db = db.getReadableDatabase();//here create a method ,and return SQLiteDatabaseObject.getReadableDatabase(); 
     Cursor curCSV = sql_db.rawQuery("SELECT * FROM "+CredentialDb.TABLE_NAME,null); 
     csvWrite.writeNext(curCSV.getColumnNames()); 

     while(curCSV.moveToNext()) 
      { 
      //Which column you want to export you can add over here... 
       String arrStr[] ={curCSV.getString(0),curCSV.getString(1), curCSV.getString(2)}; 
       csvWrite.writeNext(arrStr); 
      } 

     csvWrite.close(); 
     curCSV.close(); 
    } 
    catch(Exception sqlEx) 
    { 
     Log.e("Error:", sqlEx.getMessage(), sqlEx); 
    } 
} 

}以下のように行う列の合計を取得するための

3 ..

Cursor cursor = db.rawQuery("SELECT SUM(" + DbHelper.CART_TOTAL + ") as Total FROM " + DbHelper.CART_TABLE, null); 

if (cur.moveToFirst()) { 

int total = cursor.getInt(cursor.getColumnIndex("Total"));// get final total 
関連する問題