简体   繁体   中英

how to connect database in 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);   

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();

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:

  1. Retrieve login information, look through database for corresponding data.
  2. Start new intent and different class with main menu layout.
  3. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM