0

I have a really different situation while using same database file in android app and web app. Hope someone has idea about this problem.

I have a web application that has a database with full of data. After that, I am exporting the file to my PC and PC to android device in .sqlite format. At this point, Let's say the last primary key id of one table in database is 25.

Now, android app will use that database file to operate. In the same table more data will be inserted. Let's say 10 more. Now the last primary key id will be 35(25+10).

At the mean time, if I insert some more data into the database from web app, let's say 3 more. Then, the last primary key id will be 28(25+3) in the database of web app.

Now, I am pulling database file from android device and importing to the web app to keep records for future. The database file in the web has most of the data same as newly imported database from android device because it was exported to android app before.

Now I want to merge the both database files to make one single database file and show record in web app. For that, what I want to do is;

  • Filter data from both databases not to keep same records.
  • Manage to replace the primary key id of android app's database followed by the last primary key id of web app's database. Like 28+10 = 38 Now the newly modified database file has 38 records.

Please someone give me some technical idea because it is even hard for me to google. I am sorry if it is unclear. If it unclear, do not hesitate to ask.

bShah
  • 309
  • 1
  • 6
  • 19
  • 1
    you can check if record exist then replace else insert. – Waqar Ahmed Mar 20 '14 at 12:31
  • @wqrahd What about changing the primary key id while merging the data between two databases. – bShah Mar 20 '14 at 12:47
  • it will replace by that orignal id . and also there is method in sqlite that insert if not exist else leave it. i guess its name like insertonconflict – Waqar Ahmed Mar 20 '14 at 12:57
  • @wqrahd could you please explain a bit clearly about **it will replace by that orignal id **. I am a bit confused – bShah Mar 20 '14 at 13:16
  • if one row has id 3 and when again records come from sql server , it will replace by the same record id 3. so it will not make a difference. – Waqar Ahmed Mar 20 '14 at 17:09

1 Answers1

0

Sounds complicated, maybe you should consider moving your data handling entirely to a cloud database: A relational database shared by an android app and a website - the easy way.

Alternatively I would suggest having two id keys on the android device, namely (_id, _id_server). This way, when importing the database from the web, the _id_server is populated and known and any future updates, from phone to web, will be easily filtered as they either have no _id_server value (new insert) or if the value is there (update existing entry).

I do not like the idea of altering id's on the web, can see a lot of problematic scenarios if doing so.

Community
  • 1
  • 1
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
  • I agree your opinion. I cannot use that way. Internet connection is not allowed here. – bShah Mar 20 '14 at 12:45
  • Actually, parse.com, as I propose in the link, supports caching up to 10mb of data before performing upload. So if the device eventually get to an internet connection, it will update the database with the cached values. – cYrixmorten Mar 20 '14 at 12:49
  • I agree. And I am sure this propose will be rejected in here where I am working. The app should work totally offline. Never connected to the internet. And the web app is running in intranet. All in all, this is not what I was looking for. I am just wondering how can I replace the primary key id while merging two databases. – bShah Mar 20 '14 at 12:54
  • Fair enough, with my alternative suggestion, you do not have to alter the primary key of the web database. – cYrixmorten Mar 20 '14 at 13:44