How to implement import / export of sqlite data from / to excel in android

I am working on an application that should provide an entire architect profile. The app is almost finished. But the ultimate challenge is to be able to backup / restore. I want Backup

data in a file like this, which can be viewed in excel or simple view. And then this bakup can also be used for option Restore

.

+3


source to share


4 answers


You can create backup functions. Python has an excellent csv library that is easy to use. As well as easy integration with sqlite. Just create a function that Select * from tableToBackUp

, and each line in that select statement should be a new line in the csv



 import csv
 with open('myCSV.csv', 'wb') as csvfile:
    writer= csv.writer(csvfile, delimiter=' ',
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
    writer.writerow(rowInSQLITEQuery['columnName']  + rowInSQLITEQuery['columnName2'])

      

+1


source


My app also includes data export. I have saved the data to a SQLite database. We then saved it to an SD card and mailed it using the JavaMail API. The exported .db

one can be easily read using software such as SQLiteBrowser

which can be downloaded for free. I am assuming that you exported the database as you mentioned Excel

in your question.



Answer if you need help / links when exporting files db

to SD card.

+1


source


The exact solution may depend on how you plan to offload data from the device.

You can search: http://developer.android.com/training/basics/data-storage/files.html

In this case, you should write rows from your SQLite table as if they were rows in a .csv file that Excel can read. The below just writes a line to a file, but you can use a loop to write lines from your database line by line.

String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

      

FileProvider looks like a more complex solution, but it might be what you want if you are trying to read a file on a device from another application.

http://developer.android.com/training/secure-file-sharing/setup-sharing.html

0


source


The short answer is, there is no direct path. What I did for my application was using a third party library to create an excel document and I actually created a sheet for each table and wrote table by table. There is no "conversion method" that will do this

Another option is to copy the .db file from internal storage to the backup folder and copy it back. But no excel participation

0


source







All Articles