public int dumpToCsv ( Context ctx , SQLiteDatabase db , String suffix ) throws FileNotFoundException { int numRowsWritten = 0 ; Cursor c ; String filename = getCsvFilename ( db . getPath ( ) , db . getVersion ( ) , suffix ) ; c = db . query ( th . getTableName ( ) , null , null , null , null , null , null ) ; FileOutputStream fos ; fos = ctx . openFileOutput ( filename , 0 ) ; PrintWriter printWriter = new PrintWriter ( fos ) ; String headerRow = buildHeaderRow ( ) ; printWriter . println ( headerRow ) ; for ( boolean hasItem = c . moveToFirst ( ) ; hasItem ; hasItem = c . moveToNext ( ) ) { String csv = buildCsvRow ( c ) ; printWriter . println ( csv ) ; numRowsWritten ++ ; } printWriter . flush ( ) ; printWriter . close ( ) ; return numRowsWritten ; }
Dumps a database table to a CSV file in the default location . Returns the number of rows written to the file .