将数据库保存为 CSV 文件时,为什么最后 3 列会变成一两列?
Why last 3 columns fall into one or two when saving database as CSV file?
为什么将数据库保存为 CSV 文件时,最后 3 列变成一两列?
几乎所有的列都保存正确,除了最后 3 个
Here is the problem
代码如下:
//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);
}
}
}
您的代码传递了最后三列,因为您使用 +
而不是 ,
[= 连接它们25=] 将它们视为单独的参数
即:-
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))
};
为什么将数据库保存为 CSV 文件时,最后 3 列变成一两列?
几乎所有的列都保存正确,除了最后 3 个
Here is the problem
代码如下:
//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);
}
}
}
您的代码传递了最后三列,因为您使用 +
而不是 ,
[= 连接它们25=] 将它们视为单独的参数
即:-
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))
};