I am copying a SQLite database to a CSV file. I know the data is being
written to the database, but when i write the code to save it as a CSV
to external storage, the local variables are not being read. Any help
is appreciated:
//This is where i create the database
private void initDatabase() {
db = this.openOrCreateDatabase(DATABASE_NAME,
SQLiteDatabase.OPEN_READWRITE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS " +
TRIP_TABLE_NAME + " (TRIPNAME STRING, ORIGIN
STRING, DESTINATION
STRING, TRIPTYPE STRING);");
db.close();//Close the database
Log.i(TAG, "Database created");
}
//This is where i write to the database
private void recordTripType(){
try {
StringBuffer queryBuf = new StringBuffer();
queryBuf.append("INSERT INTO "+TRIP_TABLE_NAME+
" (TRIPNAME,ORIGIN,DESTINATION,TRIPTYPE) VALUES
(" +"'"+TRIPNAME
+"',"+
"'"+ORIGIN+"',"+"'"+DESTINATION +"',"+"
'"+TRIP_TYPE+"');");
db = openOrCreateDatabase(DATABASE_NAME,
SQLiteDatabase.OPEN_READWRITE, null);
db.execSQL(queryBuf.toString());
Log.i(TAG, queryBuf.toString());
} catch (Exception e) {
Log.e(TAG, e.toString());
} finally {
if (db.isOpen())
db.close();
}
}
//This is where i attempt to export the database
private void writeToCSV(){
SQLiteDatabase db = null;
Cursor cursor = null;
try {
db = openOrCreateDatabase(DATABASE_NAME,
SQLiteDatabase.OPEN_READWRITE, null);
cursor = db.rawQuery("SELECT * " +
" FROM " + TRIP_TABLE_NAME, null);
int tripnameColumnIndex =
cursor.getColumnIndexOrThrow("TRIPNAME");
int originColumnIndex =
cursor.getColumnIndexOrThrow("ORIGIN");
int destinationColumnIndex =
cursor.getColumnIndexOrThrow("DESTINATION");
int triptypeColumnIndex =
cursor.getColumnIndexOrThrow("TRIPTYPE");
if (cursor.moveToFirst()) {
StringBuffer fileBuf = new
StringBuffer();
String tripName = null;
String origin = null;
String destination = null;
String triptype = null;
do{
tripName =
cursor.getString(tripnameColumnIndex);
origin =
cursor.getString(originColumnIndex);
destination =
cursor.getString(destinationColumnIndex);
triptype =
cursor.getString(triptypeColumnIndex);
}while (cursor.moveToNext());
String fileContents =
fileBuf.toString();
Log.d(TAG,
fileContents);
LoginActivity.checkStorage();
File sdDir = new
File("/sdcard/OriginDestination");
sdDir.mkdirs();
File file = new
File("/sdcard/OriginDestination/"+TRIPNAME
+".csv");
try{
FileWriter OdSdWriter = new
FileWriter(file, false);
OdSdWriter.write(fileContents);
OdSdWriter.close();
}catch(IOException e){
e.printStackTrace();
}
}
} catch (Exception e) {
Toast.makeText(getBaseContext(),
"Error
trying to export: " + e.getMessage(),
Toast.LENGTH_LONG).show();
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
if (db != null && db.isOpen()) {
db.close();
}
}
}
//These four variable are not read locally:
String tripName = null;
String origin = null;
String destination = null;
String triptype = null;
Any help is greatly appreciated
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en