I have a remote SQL database (i.e. a database stored in a server). I am developing an Android app that regularly stablishes a connection to copy all the remote database's tables (and their contents) to a local database using SQLite.
Is there any standardized way to perform this action within the Android SDK?
What I've though:
I thought about requesting the remote database's contents in JSON format, then parsing that JSON code in Android, then including it in the local database using SQLite. Don't know how efficient this is for large amounts of data, neither if there exists a better (less painful) approach.
There is no built in way to do this. Nor could there really be- way too many possible ways people would want to tweak things, its something better left up to developers or 3rd party libraries.
Really the easiest way is just to build a SQLite db once an hour or so on the server and have the clients download that file. That way you don't have to do the complicated and expensive JSON parsing and SQLite insertion. Of course you're always going to be a bit out of date this way, but if that's a concern you need to question whether local caching is a good idea at all.
Although it might be theoretically possible to implement a connection to a remote database, I think your own idea makes sense. Implement a standard restful web service in front of the db, or investigate existing out of the box solutions for such a service, and return data using json. Parse the json and store it in the local sqlite.
Related
I am developing an android application. I want to update the local SQLite database with MySQL database on server. I am not able to figure out that what is the most appropriate and standardized way to do so?
Create a webservice (REST is probably best) and serialize your SQLite/MySQL data and PUT/POST/GET it to/from your web service. This will give you a nice layer of abstraction in case you decide to switch from MySQL to something else server side.
You may want to take a look at fyrecloud.com/amsler This is source code for a demonstration Android application that implements MySQL replication between a MySQL server and the SQLite db on an Android device.
Amsler rests on two pillars:
It communicates with the MySQL server using the MySQL Client/Server protocol in order to connect to the server for authentication and for receiving replication events as they occur.
It uses the Antlr lex and parse software in order to lex and parse incoming replication events and then to translate the MySQL commands into equivalent SQLite commands.
This is great for one-way replication. You can simulate two-way replication by modifying the MySQL server indirectly via RESTful type methods and then watching while MySQL sends a new replication event back.
Accessing a server via REST is easy enough. However, modifying an existing MySQL installation in order to support serialization presents too many headaches to enumerate here. Amsler takes advantage of pre-existing replication services. REST also depends upon some polling strategy in order to keep the local device reasonably up-to-date. Again, many problems with this approach. Amsler maintains a TCP/IP connection to the server which enables server-push notification of updates.
The most difficult part of Amsler is in figuring out the lexing/parsing. The Syntax between MySQL, SQLite, and the various versions of the same have many subtle differences. So many differences that it's impractical to provide a shrink-wrap translator and instead you must resort to modifying the grammar yourself.
Nevertheless, good, bad, or ugly, here it is. Take a look and maybe the glove fits.
This is probably going to be helpful: sync databases Mysql SQLite
The real answer is that there is no standard or built in magic way to just copy a MySQL database that lives on a server somewhere to a device. You will have to implement either a webservice or somehow convert the MySQL db on the server to the android sqlite implementation and download that file into your app's data directory (not a route I'd recommend taking).
Late to the party, but http://www.symmetricds.org/ is a good solution.
Java, runs on Android too.
LGPL.
Can handle 10,000's of clients.
There is no standard way. Depending on your needs you can e.g. use webservices in REST or SOAP protocols or more binary data exchange.
my application needs to store data locally on sqlite DB and user can upload data on server once the internet connection is available.
I assume I need to do some DB replication, how to accomplish that?
Is there any android built in facility?
Or I need to use some third party tool for that?
I read many answers on this but I am still not clear about it, I will use KSOAP2 web service for communicating with server.
Using KSOAP2 how can I Sync my sqlite data on SQL Server?
below are few links I read.
https://stackoverflow.com/questions/18207021/opensource-replication-tool-for-sqlite
Regarding sqlite replication
Does SQLite support replication?
Thanks in advance.
You shouldn't use dumps to copy the whole database back and forth. Android is running on mobile devices and datatransfer is likely to cost money or to be limited by speed or volumne or other means.
Therefore you should build your own webserver and upload/download only the changed entries. This means you will probably have to implement your own replication-logic, since I don't know any tools for that.
For example you can send a timestamp and the server replies with a set of changed data.
I'd recommend building a Restful-Webservice and using HttpUrlConnection to build POST, PUT, GET and DELETE requests.
GreenDao proposes a project for client/server-synchronization. But there is no estimation on when it will be available. If you do something yourself, you could try doing it as greendao-feature. A lot of people will thank you for that (myself included :D).
You can also have a look at symmetricDS, which should work on android.
What is the best way for an app to push and pull data from an external database?
I want users of my app to be able to write to the database as well as view record data.
Would I use a SqLite database on a webserver somewhere? Does the database type make a difference?
Would an XML file work?
I am thinking that I can have data in my app. So, as users write new records, other users would be able to see those updates.
Suggestions? Comments? What's the best way to do this?
I personally use a local SQLite db on the android, that uses HTTPposts to interact with php on my webserver, which then accesses a mySQL instance. Communication is done through serialized JSON objects as text, gzipped. Transfers about 50kb of raw text as 10kb compressed, very manageable.
Theres lots of excellent tutorials here and otherwise, if you do a little googling for httppost android.
It is generally recommended NOT to have your mobile device have direct access to the online database, for better security and abstraction. Besides php can do some great stuff like doubly validate inputs.
I've just written a short Android app which stores userdata in the phone-side sqlite database.
What I'd like to be able to do is to add this to an online database (I currently have a mysql database with my webhosts, but if there's any easier way then I'm open to suggestions), but it'll be subject to condition (Such as if a certain value doesn't already exist). I'd also like to be able to get data from this online database too to be added to the sqlite database on the phone.
I've had a look around and people seem to suggest using php as a go-between for that, but is that the easiest way? there aren't any mysql helper classes that could just interface directly or anything?
Newbie question I know, but the project was to teach myself how Android works so getting stuck in is the way to go..
Cheers!
Yes; using PHP is an example of an easier way to go. You need to create web services which allow you to interact between the android phone and a MySQL database. To my knowledge you can't go directly to a database hook; as you need to have something that can hook in. Also it would be a security issue if you put on each and all of your phones the connection information for your database.
Think if you had to change the host of your DB as your traffic grew large that you needed to upgrade; this would be a new update in the store and all clients would need to update this; otherwise you would be maintaining two code bases.
By using PHP you are able to create that middle level and easily interact with the DB.
Here is a quick article on creating REST PHP Web Service. Tutorial
Good Luck!
So my Android app uses a SQLite database. I need to "replicate" two or more tables with a central server. The remote server will merge the data from remote client devices (my app), along with a few extra fields to make each record unique, since multiple client devices will be participating.
This must be a common need for developers using SQL replication so I'm hoping someone can point me to an existing (turn-key) solution.
If not, I would like to consider methods that doesn't require me to code a lot of specifics about the table schema. Perhaps I could just specify the table and specify the server and that's it. I guess I could even sqlite dump to file then pass the file?
Thoughts?
Thanks!
Do you only need to send data to the central server (replication as you say), or do you also need to receive data from that server (synchronization)? If you only need to send data and bandwidth/data usage is not a constraint, then you can create a CSV or TSV file with the data you need, optionally compress it and send it to the server, and implement all merging logic in the server. Even if it's not table-specific, you need to determine what to do in case you already have identical or very similar data (overwrite, ignore, error?).
I would advise against just sending raw sqlite data, since that would create an unnecessary dependency with sqlite on the server. Creating a TSV file isn't hard.
I don't think there are any turn-key solutions for what you need, though there are many libraries and frameworks to help you implement it server-side.