Is it possible to create a content provider for a different database like MySQL or SQL. How can I achieve this? Any suggestions?
ContentProvider can be applied to any kind of data even not SQL one. If you know how to get connection to MySQL (not easy task) - you can easily implement your own ContentProvider over MySQL data - just create your own class extending ContentProvider - and you're there.
This is not possible to use android content provider for different database.Please have a look at Does the content provider allow for MySql synchronization?. As Android doesn't currently include support for automated MySql server synchronization.
The Alternate Option
Content provider can be applied to any kind of data even not SQL one. If you know how to get connection to MySQL (not easy task) - you can easily implement your own content provider over MySQL data - just create your own class extending content provider - and you're there.
But As currently Android content provider or resources are not available to connect or create content provider which can be able to server synch.
If you want to expose the data to your another app you can use content provider. Otherwise it is not needed.
Unfortunately, Android doesn't have too much support for MySQL unless you have a web server. As far as SQL Server, there may be some classes that are available for you to use but many of them are targeted at ASP.NET and Microsoft-based platforms. This page practically says it all.
Related
I'm kinda new in android development and I am little confused about content providers. Content providers are used to share your data with other application legally, but is it correct to use it for retrieving your data from SQLite databaseand display it in, for instance listView? What is better solution for displaying data from db?
Thanks
Yes it is legit using the ContentProvider to retrieve data from a SQLite database while its perfectly fine creating an Handler to handle the SQLiteOpenHelper and just using it.
Its all depends on your app's needs,
Me individually for my own app if no need for sharing data wouldn't use a content provider.
if you don't need to share the data base from your app it's not necessary to use a Content Provider
I am creating app that should have offline mode, so previously downloaded data should stored somewhere, the most common way is to store data in SQLite database.
Mostly SQLite database is used with Content Provider in android. I have clear understanding what is the purpose of content provider (to share data between different apps), but in my case application will never need to share the data with other apps in the system.
Content provider has the similar interface as HTTP request (GET,POST,PUT,DELETE).
My idea is to create facade class which can be used like this getAllLatestNews(); firstly it will try to get latest data from the internet, if it fails - data from database will be used and if request is successful it also will save retrieved data to the database. This class will be facade for separating different layers of application (not to make requests from activities directly).
But now I am a little bit puzzled deciding whenever I need Content Provider or not. I can use SQLiteOpenHelper classes to retrieve and save data to the database or even use ORM library to do this.
At first I wanted to implement REST API Pattern B by Virgil Dobjanschi. But now I am not sure about this, maybe it would be better to create facade for Robospice(in my case, network request in the service) requests and do persistence there ?
Please share you thoughts about this topic, I would be grateful for any help.
EDIT
I asked this question because I feel that it is not good practice to make requests directly from activities even if they are made in service under the hood, I want to separate different layers of my application in order to make it more flexible and maintainable.
As you don't intend to share your data, i would say that implementing a ContentProvider is overkill.
Personally im a huge fan of ORM libraries (Currently i use SugarOrm in several projects), so i would go down that road.
Then at app startup, you check whether or not you have an active internet connection, and based on that you either get the latest information online, or retrieve older information from the database.
To seperate the logic a bit, i would most likely implement the getting of online information in a service, which would then store it in the database and broadcast to the activity that the information is now available, and then the activity could retrieve the information from the newly updated database.
Content Providers are absolutely for sharing data between applications and are of no use without this purpose.
If you want to use those data only in your app privately, you could use SQLite databases. Also there other objects available:
Shared Preferences
Files
Content provider has the similar interface as HTTP request (GET,POST,PUT,DELETE)
I don't think so. It's more like to SQL language.
I was wondering whether we can use the databases like (contacts.db, mmssms.db) directly instead of content providers ?
I have a reason to ask so. In my recent project, I was supposed develop a contact app. I used content provider for contact management. As I have learnt via content providers, I can query only table at a time via URIs, there is no way (at least, I didn't find) to join two tables and then get a query resolved.
And I had read, that databases are only visible to the applications that originally created them, so do my app would be able to access these databases ?
I am just a hobby developer for my own phone. I have no intention to make an app that directly uses the databases. I can pull the database from device, analyse them via sqlite. It is not that I am up against the content providers or they don't suit my need write now, it is just that it can be done or not ?
Any opinions ?
Content Providers give you a lot more than just database access. Being able to use Loaders to automatically reload your data (and update your UI) when the underlying data changes can vastly simplify your applications.
The goal of Content Providers is more to create a single, controlled layer for accessing your data. There is nothing stopping you from creating a custom URI that joins multiple tables together and returns the resulting joined result.
What's the purpose of Content Provider? The document here said:
Content providers store and retrieve data and make it accessible to all applications.
I just want to store my data into Sqlite, no need to share with other application, shall I still need to use Content Provider API?
Sqlite is an Android wrapper for using SQLite DB's. It provides neat and cleaner interface for all DB operations. Ofcourse one of the major pros for using Content providers is it provides mechanism for sharing your data with other apps.
But surely even for storing and retrieving data in db, Content provider provides a nice interface and it is highly recommended.
There are other classes like SqliteDBHelper using which you can perform direct operations on DB.
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!