データベースをCSVファイルとして保存するときに、最後の3列が1つまたは2つに分類されるのはなぜですか?
最後の3つの列を除いて、ほとんどすべての列が正しく保存されました
コードは次のとおりです。
//export DB to CSV file
public static String currentFilePathOfsavingCSVtoSD;
public static void exportDBtoCSVsaveonSD() {
String currentDateString = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(Calendar.getInstance().getTime());
File dbFile = new File("/data/data/" + "com.exactly.scooltr.inventoryAppBasic" + "/databases/" + DATABASE_NAME);
File exportDir = new File(Environment.getExternalStorageDirectory(), "/" + main_folder+ "/CSV_files/");
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, "("+currentDateString+")"+"simpleinventorycsv.csv");
currentFilePathOfsavingCSVtoSD = file.toString();
try {
file.createNewFile();
CSVWriter csvWrite = new CSVWriter(new FileWriter(file));
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Cursor curCSV = db.rawQuery("SELECT " + ProductContract.ProductEntry.COLUMN_PRODUCT_NAME+", "+ProductContract.ProductEntry.COLUMN_PRODUCT_PRICE+", "+
ProductContract.ProductEntry.COLUMN_PRODUCT_QUANTITY+", "+ProductContract.ProductEntry.COLUMN_PRODUCT_SOLD+", "+
ProductContract.ProductEntry.COLUMN_PRODUCT_BARCODE+", "+ProductContract.ProductEntry.COLUMN_PRODUCT_LOCATION+", "+
ProductContract.ProductEntry.COLUMN_PRODUCT_CATEGORY+", "+ProductContract.ProductEntry.COLUMN_PRODUCT_NOTE +
" FROM " + ProductContract.ProductEntry.TABLE_NAME , null);
csvWrite.writeNext(curCSV.getColumnNames());
while (curCSV.moveToNext()) {
//Columns I want to export
String arrStr[] = {curCSV.getString(0),curCSV.getString(1), curCSV.getString(2),curCSV.getString(3),curCSV.getString(4),curCSV.getString(5)+curCSV.getString(6)+curCSV.getString(7)};
csvWrite.writeNext(arrStr);
}
successSavingAsCSV=true;
csvWrite.close();
curCSV.close();
} catch (Exception sqlEx) {
Log.e("ProductProvider", sqlEx.getMessage(), sqlEx);
}
}
}
コードは、最後の3つの列を個別の引数として扱うのでは+
なく、を使用,
して連結しているため、最後の3つの列を渡します。
すなわち:-
String arrStr[] = {
curCSV.getString(0),
curCSV.getString(1),
curCSV.getString(2),
curCSV.getString(3),
curCSV.getString(4),
curCSV.getString(5)+curCSV.getString(6)+curCSV.getString(7)
};
する必要があります:-
String arrStr[] = {
curCSV.getString(0),
curCSV.getString(1),
curCSV.getString(2),
curCSV.getString(3),
curCSV.getString(4),
curCSV.getString(5), //<<<< comma instead of +
curCSV.getString(6), //<<<< comma instead of +
curCSV.getString(7)
};
ただし、以下は、ハードコードされた列インデックスオフセットとは対照的に、計算されたオフセットを使用するため、はるかに回復力があります:-
String arrStr[] = {
curCSV.getString(curCSV.getColumnIndex(ProductContract.ProductEntry.COLUMN_PRODUCT_NAME)),
curCSV.getString(curCSV.getColumnIndex(ProductContract.ProductEntry.COLUMN_PRODUCT_PRICE)),
curCSV.getString(curCSV.getColumnIndex(ProductContract.ProductEntry.COLUMN_PRODUCT_QUANTITY)),
curCSV.getString(curCSV.getColumnIndex(ProductContract.ProductEntry.COLUMN_PRODUCT_SOLD)),
curCSV.getString(curCSV.getColumnIndex(ProductContract.ProductEntry.COLUMN_PRODUCT_BARCODE)),
curCSV.getString(curCSV.getColumnIndex(ProductContract.ProductEntry.COLUMN_PRODUCT_LOCATION)), //<<<< comma instead of +
curCSV.getString(curCSV.getColumnIndex(ProductContract.ProductEntry.COLUMN_PRODUCT_CATEGORY)), //<<<< comma instead of +
curCSV.getString(curCSV.getColumnIndex(ProductContract.ProductEntry.COLUMN_PRODUCT_NOTE))
};
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加