I'm working on building the ability to import data via CSV file into a SQLite database table within an Android app UI.
There's a button with an OnClickListener that allows the user to select the file they want to upload. After the file is selected, they are returned to the "Import Accounts" page that also has an "Import CSV" button at the bottom of the page.
ImportAccountActivity java code for the OnClickListener:
btn_choose_file.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
}});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICKFILE_RESULT_CODE:
if (resultCode == RESULT_OK) {
FilePath = data.getData().getPath();
Uri returnUri = data.getData();
returnCursor = getContentResolver().query(returnUri, null,
null, null, null);
/*
* Get the column indexes of the data in the Cursor,
* move to the first row in the Cursor, get the data,
* and display it.
*/
nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
textFile.setText(returnCursor.getString(nameIndex) + " has been selected!");
}
break;
}}
// Import CSV button OnClickListener
btn_csv_import.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Access Database to be writable
SQLiteOpenHelper database = new AccountDbHelper(getApplicationContext());
SQLiteDatabase db = database.getWritableDatabase();
FileReader file = new FileReader(fileName);
BufferedReader buffer = new BufferedReader(file);
String line = "";
String tableName = AccountLogin.AccountEntry.TABLE_NAME;
String columns = "_id, account_title, user_name, account_password, account_notes";
String str1 = "INSERT INTO " + tableName + " (" + columns + ") values(";
String str2 = ");";
db.beginTransaction();
while ((line = buffer.readLine()) != null) {
StringBuilder sb = new StringBuilder(str1);
String[] str = line.split(",");
sb.append("'" + str[0] + "',");
sb.append(str[1] + "',");
sb.append(str[2] + "',");
sb.append(str[3] + "'");
sb.append(str[4] + "'");
sb.append(str2);
db.execSQL(sb.toString());
}
db.setTransactionSuccessful();
db.endTransaction();
}
});
I want the user to then be able to press the "Import CSV" button and have the data inserted into the SQLite database table. However - I'm having issues with this part, specifically what I need to have inside the btn_csv_import.setOnClickListener. I tried viewing this StackOverflow question, however I'm not quite sure how to get the line fileName correctly for the line that says FileReader file = new FileReader(fileName);
Or - if anyone has a better way to import the CSV file into the SQLite Database, then I'd love some assistance on this, thank you!!
Let me know if there's any other details that I can add to help make this more clear.