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.
Related
I started developing an app that uses sqlite that is to be installed on multiple devices and any updates done on sqlite database from any device are to be reflected in other devices as well. I have researched a little and found that Sqlite DB is local to a device and changes done in one device are not reflected in others and for that I have to use external server. Is there any way around it?
Is it optimal to directly store data in external server or use sqlite and sync it regularly with external database?
Thanks in advance
As far as I know, there isn't a way without an external database. I would recommend you to sync it regularly with an external database.
Checkout this question for more information how to do that:
How to sync SQLite database on Android phone with MySQL database on server?
Answer of Andrew White
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 will achieve your goal using external server. It's not necessary to create your own server, just use data store services like Parse. For more look here.
You can use data directly from external server or cache them on your device first (sqlite, prefs, json etc.) – it's up to you.
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.
I'm making a simple GPA android app. The user can input their grades and class names for each semester. How would I then store each of these semesters so that they can always be pulled up in the app? I might also need to store random variables that are alone.
I've briefly looked at options such as Shared Preferences, Internal Storage, and others. What option is the best for my needs? Please explain why. Thanks!
Here is Explanation...
Shared preferences are good for storing ... an application's preferences, and other small bits of data. It's a just really simple persistent string key store for a few data types: boolean, float, int, long and string. So for instance if my app had a login, I might consider storing the session key as string within SharedPreferences.
Internal storage is good for storing application data that the user doesn't need access to, because the user cannot easily access internal storage. Possibly good for caching, logs, other things. Anything that only the app intends to Create Read Update or Delete.
External storage. Great for the opposite of what I just said. The dropbox app probably uses external storage to store the user's dropbox folder, so that the user has easy access to these files outside the dropbox application, for instance, using the file manager.
SQLite databases are great whenever you a lot of structured data and a relatively rigid schema for managing it. Put in layman's terms, SQLite is like MySQL or PostgreSQL except instead of the database acting as a server daemon which then takes queries from the CGI scripts like php, it is simply stored in a .db file, and accessed and queried through a simple library within the application. While SQLite cannot scale nearly as big as the dedicated databases, it is very quick and convenient for smaller applications, like Android apps. I would use an SQLite db if I were making an app for aggregating and downloading recipes, since that kind of data is relatively structured and a database would allow for it to scale well. Databases are nice because writing all of your data to a file, then parsing it back in your own proprietary format it no fun. Then again, storing data in XML or JSON wouldn't be so bad.
Network connection refers to storing data on the cloud. HTTP or FTP file and content transfers through the java.net.* packages makes this happen.
Considering this i suggest you to use Sqlite especially in your case.
Best luck
it depends on your need, some times you use all options in the same app,
for example : the best way to store grades and classes is using database, in android SqlLite database.
and for storing some variables values like username and password you just need to use shared preferences.... at least this is my policy in my apps.
SQLite will be the best for your scenario.
As you can create well formatted Tables with desired columns. Either you can use pre-developed database or you can create tables on the go.
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.