Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

James Smith
PLUS
James Smith
Courses Plus Student 454 Points

I already have a website. How do I use android app to access my existing database in mysql? is that possible?

The site is currently written in PHP for server side. How do I navigate (INSERT, UPDATE or read only etc.) through the existing Mysql database using Android-Java.

Or do I need to replicate the mysql database onto android-java platform.

Folks I am very new to this android programming, so my question may be elementary.

4 Answers

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi James,

One way to get information from your website to your android application would be to set up a RESTful service with your website. From there you can read, insert, update information via HTTP. I'm not sure how much you know about those two things... but the question is not exactly "simple"

James Smith
PLUS
James Smith
Courses Plus Student 454 Points

Thanks Jon for replying,

I know this would not be easy. I will keep on with the course on the client side. Once I get enough knowledge on that front, I will turn to the server side. Meanwhile, doe the android mobile has its own database called sqlite.

Also, does treehouse get into that much detail about server side programming. Especially for someone with an already existing website.

Again thanks for anserwing the question.

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

Yes, Android has it's own database -- SQLite. However, this is local to each individual phone. I imagine you are looking for something in which you can "connect" many devices/users via the internet.

I imagine Treehouse does get into more detail about server side programming. I'm not sure which track would be best, but as you continue the current track they often tell you the best next steps.

Let's see if this helps:

In your example website you have a database in which you have a list of paintings to sell. When you display the paintings for sale on your website you may go the the link: www.jamessmith.com/paintings

That is a link that gets your list of paintings and displays them in HTML. You can do the same in Android via the browser, but if you want the data to go to your app... you still ask for data via HTTP/HTTPS except you want the list of paintings formatted differently (not in HTML). So you can set up your website to respond to something like: www.jamessmith.com/paintings.json

This would give you your list of paintings in a format called JSON, which is more friendly for app use because it gives the raw data and doesn't worry about the styling (text, colors, etc). You use that link in your Android app and your app will receive the data. You can do whatever you want with the data afterwards within your app (show to your users in a ListView, etc).

James Smith
PLUS
James Smith
Courses Plus Student 454 Points

WOW - thanks Jon for the great and detailed information. I am very thankful for this forum. However, it looks like I have a lot of work/study/practice to do.

Also, speculating on what I think I may find or come across in the near future. Is it recommended to replicate data from mysql into SQLite, in case user is offline or no mobile signal?

Another question, since my site is 99% social networking and since users will constantly need to access mysql database for 99% of their interaction, why would I want to use SQLite? Would it be for speed?

Thanks again

Jon Kussmann
Jon Kussmann
Courses Plus Student 7,254 Points

I would probably not attempt to replicate your MySQL database into SQLite (if you mean MySQL from your site to SQL on an Android device). There are caching mechanisms for Android you can take advantage of, but I wouldn't do so for an entire database (maybe just your news feed, recent messages, etc for example). You probably wouldn't want to replicate all friends/their profiles, etc since it would take up a lot of data.

You could SQLite again for caching, but for the most part you would be taking advantage of the internet/your website to get the most up to date information.

Say user A sends a message to user B. A can have that message saved into SQLite into their Android device, but B will not "receive" that message. So, the message would need to be sent to your web application first and then to user B. This can be done without saving to SQLite initially, but your MySQL is a necessary step.

However:

Say a user wants to update their profile information for their friends to see, but his/her device is currently not connected to the internet. So could save that new information to an SQLite database on their phone and when the device finally gets connected to the internet, it could automatically "push" that new information to your web application without making your user re-write their new profile again.

There aren't too many "absolutes" for this kind of thing, I would think more about components and how you want to save/retrieve information.

James Smith
PLUS
James Smith
Courses Plus Student 454 Points

As I am reading your message, I am noticing that this would have to be on a case by case basis. So, information I would want a user to have in their SQLite mobile will depend on the type of info and our much information. And you are correct here there aren't too many absolute. This is would probably be on a case by case basis by experimenting.

As for this course, I am finding myself going back to previous lesson many times. So this journey will not be easy.

Thanks