0

So I created first database, and now I need to create separate database, zipped it and send it to server.

I'm just wondering if it is doable?

And how can I do it?

9
  • So you created a sqlite database in your app, now you want to create another one, export it, and send it to a server. Are the two databases related at all? Commented Feb 10, 2016 at 19:04
  • @Ethan no, but I will write parcel data from db1 to db2. Also I want to destroy db2 after sent to server. Commented Feb 10, 2016 at 19:22
  • So you don't want to create a separate database....you just want to pull some data from db1 and send it to the server. Commented Feb 10, 2016 at 19:23
  • However, the file type has to be SQLite Commented Feb 10, 2016 at 19:25
  • Okay, so run your query(s) to get the data you want from db1, create db2, insert everything from your query(s) to db1 into db2 via the Cursor's you get back, then get the path to db2 (there's a getPath() method on database) and then send that file. Then delete db2. Commented Feb 10, 2016 at 19:30

1 Answer 1

1

Here's the basic logic of what you want to do, it'll have to be supplemented with specific code from here: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

and here: https://developer.android.com/reference/android/database/Cursor.html

and here: https://developer.android.com/reference/android/content/ContentValues.html

SQLiteDatabase database1 = SQLiteDatabase.openDatabase(<PATH TO DATABASE1>, null, 0);
SQLiteDatabase database2 = SQLiteDatabase.openOrCreateDatabase(<WHEREVER YOU WANT DATABASE2 TO GO>, null);
Cursor cursor = database1.query(<You'll really just have to fill this in with what you're pulling from database1, it's way too specific>);
ContentValues contentValues;
while (cursor.moveToNext()) {
    contentValues = new ContentValues();
    contentValues.put(<Column key>, <Column value from cursor>)
    // I.E. contentValues.put(ID_KEY, cursor.getString(cursor.getColumnIndex(ID_KEY));
    // <INSERT THE REST OF YOUR COLUMNS INTO contentValues>
    database2.insert(<TABLE_NAME>, null, contentValues);
}

cursor.close();
database1.close();
database2.close();

To send it, that depends on way too many things for me to address. You have the database file now, send it using whatever protocol you please. If you don't know how to do this, search for "Android send File to Server" (try with an without quotes).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this is very helpful!! I got the sending part cover.
Great! Happy to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.