gpt4 book ai didi

android - 将数据库保存为 CSV 文件时,为什么最后 3 列会变成一两列?

转载 作者:行者123 更新时间:2023-11-30 00:08:23 24 4
gpt4 key购买 nike

为什么在将数据库保存为 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);
}
}


}

最佳答案

您的代码通过了最后三列,因为您使用 + 而不是使用 , 将它们视为单独的参数

即:-

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))
};

关于android - 将数据库保存为 CSV 文件时,为什么最后 3 列会变成一两列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48586914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com