how to connect database in android? - android

I am newbie in creating android app. I have created a database using xammp and a simple login layout using eclipse. Now I want to connect my login page and the database I created so that when the user enters his/her username and password it will then open the main menu of the application. I am hoping for someone who could help me on how to do this. Thanks in advance.

Try this code .....it will help u
data = openOrCreateDatabase(
"AutoProfiles.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
data.setVersion(1);
data.setLocale(Locale.getDefault());
data.setLockingEnabled(true);
final String CREATE_TABLE_LOCATIONS =
"CREATE TABLE IF NOT EXISTS tables ("
+ "name TEXT)";
data.execSQL(CREATE_TABLE_LOCATIONS);

YOur question is a bit confusing... Where does the database get into the Picture. Is the database used to store login information to determine which user is currently logged in?
If this is the case:
Retrieve login information, look through database for corresponding data.
Start new intent and different class with main menu layout.
Link the information in your database to your main menu

I am actually working on a similar project with my application. Here is a simple tutorial that has helped me alot with my project. It explains alot about how a database works in android using eclipse and how to set one up, add, remove, update and get data from that database then.
http://www.smashingmagazine.com/2011/03/28/get-started-developing-for-android-with-eclipse-reloaded/
Hope it helps you as much as it helped me

If you want to connect to a Mysql database you may need to use webservice such as PHP .
Here is how you do it :
You create a Mysql database
You create a PHP file which connects to database and does the data fetching and stuff
Using an AsyncTask use URL methods to acess the remote PHP file
Analyse the return result
The PHP file can either take GET or POST arguments
Example Snippet :
URL url = new URL("http://127.0.0.1/project/check_reg.php?uname="+email.getText().toString()+"&pwd="+password.getText().toString());
URLConnection urlcon = url.openConnection();
BufferedReader bf = new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
String xresponse = bf.readLine();

Related

Android data need to insert in database dynamically

We are getting the request from Android app as json array. For example:
$var = [{"username":"kp","mailid":"kp#gmail.com","phoneno":"9876543563","groupname":"android"}]';
I want to make this static data (kp, kp#gmail.com, 9876543563, android) as dynamic to insert the values into database.
In what method we are getting the data? And from an android app, is it POST or GET variable, or an other method? Please let me know how to make this value dynamic which is coming from the android app.
How to do you want to send the data from Android App? Is it needed to be namely JSON-format?
I think the most useful case is to use usual, e.g., POST data and retrieve this using base methods to save it inside PHP. You can use, of course, GET format for 'short' request.
For instance,
Android sends using GET: http://yourcompany.com/senddata?username=name&mailid=e#yourmail.com
You PHP code (very-very-very simple approach):
/// ... initialization...
$sql = "INSERT INTO users (username, email) VALUES ('". ($_POST["username"]) . "', '". ($_POST["mailid"]) . "')";
mysqli_query($conn, $sql)
PHP insert example
To create POST or GET request in Android I would recommend to use JSOUP library
Download
A simple example for jsoup:
Document doc = Jsoup.connect("http://yourcompany.com/url?var1=a").get();

How do I add Bookmark option in WebView?

I am new to Android app development, I have a simple app which works on WebView to open a website. I want to add "bookmark this webpage" option in my app but I don't know how to proceed.
You can add any button to your app. Or if you are using Toolbar you can add bookmark button to your options and you can save the page url to Sqllite database or any other storage. There are many other ways also.
you can have a look at the following links-
First- Second-
I hope this would help you.
Well you can create a class for that extends SQLLiteOpenHleper. This provides functionality for accessing sqllite database-
You can refer to this and this link for SQlLite..
And you can create a query for creating a table having columns like id, bookmark name and bookmark url.
You can also add some functions for setting name and url, getting name and urls etc.
I hope you find this helpful.

Getting data from phpmyadmin and transfer each item to a textview in eclipse

in need of help on this kind of matter,i just wanted some tips and thoughts about it..
example:
i have a table named listOfPeople and inside are the following:
-idnum
-name
-age
-location
then in eclipse,
i have 4 textviews and 1 button,
then, if i pressed the button, the NAME,AGE and LOCATION will show in the 3 textviews with a specific idnum
Persons ID Number : 1
Name of person : sample sample
Age of person : 14
Persons location : sample area
what should i do? i have a jsonParser running up, a defaulthttpclient, xampp as my connector and phpmyadmin as my database.
in my php code (select.php) :
$result = mysql_query("SELECT * FROM listOfPeople WHERE idnum='$idnum'");
You cannot connect to a mysql database from an Android device directly. You have to run a web service on your server which fetches the data from the database (I see you already have a php script so I suggest using that one) and convert that to JSON and send that back to your Android device.
In your app you can parse the incoming JSON to separate strings and set these strings to the appropriate TextViews using setText(string)

Inserting a hyperlink from a SQLite database to Android app

I have an android app which pulls text data from a SQLite database (located in /assets/ folder). The problem is, if i store a hyperlink, say http://example.com in my SQLite database, it is rendered as normal text in my android app. It's not clickable. How can i pull a clickable hyperlink from SQLite database and show it in my app?
If it's a TextView, include the following in the definition:
android:autoLink="web"
Use Linkify for transforming text containing links into text with clickable links.
The URL stored in your SQLite DB is just text like you mentioned. If I understand your question what you want is to trigger the browser and open the URL you have stored in your DB?
If so, you can simply query the URL from the SQLite DB and do the following to display it in the default browser.
String url = 'http://example.com'; // Get it from your DB as you are doing now.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);

Create list in android app

I want to know a good way to create a list in my android app. I have all info in my DB and want to load data from it each time I start the app and make a list from it (id and title).
What is the best approach?
Should I make a PHP-script that responds with a JSON encoded array with all list items or should I make an XML-file that generates each time the data in the DB changes that I import to the app each time it starts? or any other good way to do it?
Since all stuff are made by XML-files in android it feels like importing a XML would be a good thing, is it? And how do I import an XML-file from a web server into the app?
// Daniel
You can use either JSON or XML.
You can use the web service approach or you can include your db with your application.
In fact, I most often choose to create a sqlite3 database of my data and include it in the assets folder, which can be copied to the app's data folder on startup.
As for copying your sqlite3 database from assets/ to the db data directory, I found these instructions helpful.
In your situation I would pick JSON over XML for all the reason's stated in the following post: http://ajaxian.com/archives/json-vs-xml-the-debate
Plus, in android, there are JSON Array's built in by default so you don't have to do any extra passing of the code.
return new JSONArray("my json string goes here...");
Since we are talking about a mobile device, I would always generate changes in your php script rather than have a full sync as this will be a lot smaller in size that a full sync. However, you will need to give your user a option to do a full re-sync if this is applicable to your app. I would use a SQLite database to store the data and only update the changes in that.
To also make the stream smaller, you can gzip compress your output from php as this can be natively read by the android device. In my app, I compress 500kb down to ~110kb before transmitting, a huge saving on performance. Here a partial example of how to read the stream:
InputStream in = null;
HttpURLConnection httpConn = null; // you will have to write your on code for this bit.
if (httpConn.getContentEncoding() != null)
{
String contentEncoding = httpConn.getContentEncoding().toString();
if (contentEncoding.contains("gzip"))
{
in = new GZIPInputStream(httpConn.getInputStream());
}
}
else
{
in = httpConn.getInputStream();
}
I hope that this all makes sense, it's been a long day programming :)
Stu

Categories

Resources