Brumma Gem
Brumma Gem

Reputation: 31

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

I am working on an app which needs to give all architect's profile. The app is almost finished. But the final task is about having backup/restore option. I want to Backup the data in such a file that can be viewd in excel or some simple view. And then that bakup could also be used for Restore option.

Upvotes: 3

Views: 786

Answers (4)

Jay
Jay

Reputation: 2686

You could create functions for backing up. Python has a great csv library which is easy to use. As well as easy sqlite integration. Just create a function that Select * from tableToBackUp and each row in that select statement should be a new row 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'])

Upvotes: 1

Snake
Snake

Reputation: 14658

The short answer, there is no straight way. What I did for my app is use 3rd party library to create excel document and I actually created a sheet for every table and wrote them table by table. There is no "convert method" out there that will do that

The other option is to copy the .db file from internal storage to backup folder and copy it back.. But no excel involved

Upvotes: 0

diszonant
diszonant

Reputation: 91

My app is also involves exporting data. I have saved the data in a SQLite database. Then stored it to the SD Card and mailed using JavaMail API. The exported .db can be easily read using software like SQLiteBrowser which is free to download. I assume that you have exported a database as you mentioned Excel in your question.

Reply if you need help/links on exporting db files to the SD card.

Upvotes: 1

whodell
whodell

Reputation: 46

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

You might be looking for this: http://developer.android.com/training/basics/data-storage/files.html

In which case you would write rows from your SQLite table as though they were rows in a '.csv' file that Excel can read. The below just writes a string to a file, but you can use a loop to write rows from your database line by line instead.

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

A FileProvider looks like a more complicated solution but might be what you want if you're trying to read the file on the device from another app.

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

Upvotes: 0

Related Questions