将SQLite数据库写入文本文件时遇到困难。
该应用程序的工作原理是将csv文件导入本地数据库。该数据库用于收集设备上的数据,然后用户将数据导回到csv文件中,以便收集回主Access数据库。
如果导入了csv文件,然后立即导出它就可以正常工作。但是,只要在数据库中创建了新条目,导出就会创建一个空的txt文件。
我认为问题是数据库中的空白值保存为 Null 值,并且将它们写入文本文件存在问题。我只是不确定是否正确的响应是更改集合,以便它将写入空白条目而不是 Null 值,或者是否有写 Null 作为csv文件中的空白值。
供参考,这是我的导出代码。
try {
Log.d("try", "initiated");
cursor = db.query("PROD",
new String[]{"_id", "PUMPER_NO", "WELL_ID", "TANK1_INV", "TANK2_INV", "TANK3_INV", "PROB1", "PROB2", "WATER_RMVD", "OSALE1_IN", "OSALE1_DATE", "OSALE1_TK", "OSALE1_BBL",
"OSALE2_IN", "OSALE2_DATE", "OSALE2_TK", "OSALE2_BBL", "GAGED_FLUID", /*"DIFF",*/ "MCFD", "MCF_CUM", "TUBING", "CASING", "LINE", "CYCLES", "CYC_TIME", "CYC_DAYS", "DAYS_OFF",
"HRS_PUMPED", "COMMENTS", "MTRIX", "DTF", "TMF", "XTRA1", "XTRA2"},
null, null, null, null, null);
int rowcount = 0;
int colcount = 0;
Log.d("FileWriter", "initiated");
File saveFile = new File(Environment.getExternalStorageDirectory() + "/Wellpro/exportprod.txt");
FileWriter fw = new FileWriter(saveFile, false);
BufferedWriter bw = new BufferedWriter(fw);
rowcount = cursor.getCount();
colcount = cursor.getColumnCount();
Log.d("Looping", "initiated");
if (rowcount > 0) {
cursor.moveToFirst();
for (int i = 0; i < colcount; i++) {
if (i != colcount - 1) {
bw.write(cursor.getColumnName(i) + ",");
} else {
bw.write(cursor.getColumnName(i));
}
}
bw.newLine();
for (int i = 0; i < rowcount; i++) {
cursor.moveToPosition(i);
for (int j = 0; j < colcount; j++) {
if (j != colcount - 1) {
bw.write(cursor.getString(j) + ",");
} else {
bw.write(cursor.getString(j));
}
}
bw.newLine();
}
bw.flush();
Toast.makeText(this, "Data Exported", Toast.LENGTH_LONG);
}
} catch (Exception ex) {
if (db.isOpen()) {
db.close();
}
} finally {
cursor.close();
db.close();
}
这是两个条目。没有 Null 值的那个来自原始CSV文件,第二个条目是在程序中创建的那个。在创建第二个条目之前,导出工作正常。
"8" "8" "1255" "ALESI-OVINGTON #2" "79.0" "0.0" "0.0" ";" ";" "0.0" "" "" "" "" "" "" "" "" "0.0" "NULL" "4.68" "28.08" "0.0" "0.0" "60.0" "0.0" "0.0" "0.0" "0" "0.0" "Not pumping" "2.0" "2016/09/26" "" "6" "" "NULL" "NULL"
"124" "124" "NULL" "ALESI-OVINGTON #2" "81.0" "0.0" "0.0" "NULL" "NULL" "0.0" "NULL" "NULL" "NULL" "NULL" "NULL" "NULL" "NULL" "NULL" "2.0" "10.0" "10.47" "227.01" "0.0" "0.0" "60.0" "0.0" "0.0" "0.0" "0" "0.0" "" "NULL" "2016/10/15" "NULL" "NULL" "NULL" "1.0" "1.0"
提前感谢您的帮助!