1. Download our Official Android App: Forums for Android!

Apps how to insert data and check the data from database and move on

Discussion in 'Android Development' started by currycrab, Sep 26, 2011.

  1. currycrab

    currycrab Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Sep 11, 2011

    Sep 11, 2011
    31
    0
    15
    programming student
    somewhere
    hi im stuck again :(

    this is my code. can anyone kindly teach me where do i insert my data into database so that when i click on the login button it will check the username and password typed by the user with the database. if its correct it will move on the menu page.

    my code below

    Code (Text):
    1. package com.logintesting;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.view.View.OnClickListener;
    7. import android.widget.Button;
    8. import android.widget.EditText;
    9. import android.widget.Toast;
    10.  
    11. public class LogintestingActivity extends Activity {
    12.    
    13.     @Override
    14.    
    15.             protected void onCreate(Bundle savedInstanceState) {
    16.    
    17.                     // TODO Auto-generated method stub
    18.    
    19.                     super.onCreate(savedInstanceState);
    20.    
    21.                     setContentView(R.layout.main);
    22.    
    23.                     final EditText txtUserName = (EditText)findViewById(R.id.txtUsername);
    24.    
    25.                     final EditText txtPassword = (EditText)findViewById(R.id.txtPassword);
    26.    
    27.                     Button btnLogin = (Button)findViewById(R.id.btnLogin);
    28.    
    29.                     btnLogin.setOnClickListener(new OnClickListener(){
    30.    
    31.      
    32.    
    33.                             @Override
    34.    
    35.                             public void onClick(View v) {
    36.    
    37.                                     String username = txtUserName.getText().toString();
    38.    
    39.                                     String password = txtPassword.getText().toString();
    40.    
    41.                                     try{
    42.    
    43.                                             if(username.length() > 0 && password.length() >0)
    44.    
    45.                                             {
    46.    
    47.                                                    db dbUser = new db(LogintestingActivity.this);
    48.    
    49.                                                     dbUser.open();
    50.    
    51.                                                    
    52.    
    53.                                                     if(dbUser.Login(username, password))
    54.    
    55.                                                     {
    56.    
    57.                                                             Toast.makeText(LogintestingActivity.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
    58.    
    59.                                                     }else{
    60.    
    61.                                                             Toast.makeText(LogintestingActivity.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
    62.    
    63.                                                     }
    64.    
    65.                                                     dbUser.close();
    66.    
    67.                                             }
    68.    
    69.                                            
    70.    
    71.                                     }catch(Exception e)
    72.    
    73.                                     {
    74.    
    75.                                             Toast.makeText(LogintestingActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
    76.    
    77.                                     }
    78.    
    79.                             }
    80.    
    81.                            
    82.    
    83.                     });
    84.    
    85.             }
    86.    
    87.     }

    Code (Text):
    1. package com.logintesting;
    2.  
    3. import java.sql.SQLException;
    4.  
    5. import android.content.ContentValues;
    6. import android.content.Context;
    7. import android.database.Cursor;
    8. import android.database.sqlite.SQLiteDatabase;
    9. import android.database.sqlite.SQLiteOpenHelper;
    10. import android.util.Log;
    11.  
    12. public class db {
    13.  
    14.    
    15.     public static final String KEY_ROWID = "_id";
    16.    
    17.         public static final String KEY_USERNAME= "username";
    18.    
    19.         public static final String KEY_PASSWORD = "password";    
    20.    
    21.         private static final String TAG = "DBAdapter";
    22.    
    23.        
    24.    
    25.         private static final String DATABASE_NAME = "usersdb";
    26.    
    27.         private static final String DATABASE_TABLE = "users";
    28.    
    29.         private static final int DATABASE_VERSION = 1;
    30.    
    31.      
    32.    
    33.         private static final String DATABASE_CREATE =
    34.    
    35.             "create table users (_id integer primary key autoincrement, "
    36.    
    37.             + "username text not null, "
    38.    
    39.             + "password text not null);";
    40.    
    41.            
    42.    
    43.         private Context context = null;  
    44.    
    45.         private DatabaseHelper DBHelper;
    46.    
    47.         private SQLiteDatabase db;
    48.    
    49.      
    50.    
    51.         public db(Context ctx)
    52.    
    53.         {
    54.    
    55.             this.context = ctx;
    56.    
    57.             DBHelper = new DatabaseHelper(context);
    58.    
    59.         }
    60.    
    61.            
    62.    
    63.         private static class DatabaseHelper extends SQLiteOpenHelper
    64.    
    65.         {
    66.    
    67.             DatabaseHelper(Context context)
    68.    
    69.             {
    70.    
    71.                 super(context, DATABASE_NAME, null, DATABASE_VERSION);
    72.    
    73.             }
    74.    
    75.      
    76.    
    77.             @Override
    78.    
    79.             public void onCreate(SQLiteDatabase db)
    80.    
    81.             {
    82.    
    83.                 db.execSQL(DATABASE_CREATE);
    84.    
    85.             }
    86.    
    87.      
    88.    
    89.             @Override
    90.    
    91.             public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    92.    
    93.             {
    94.    
    95.                 Log.w(TAG, "Upgrading database from version " + oldVersion
    96.    
    97.                         + " to "
    98.    
    99.                         + newVersion + ", which will destroy all old data");
    100.    
    101.                 db.execSQL("DROP TABLE IF EXISTS users");
    102.    
    103.                 onCreate(db);
    104.    
    105.             }
    106.    
    107.         }    
    108.    
    109.        
    110.    
    111.        
    112.    
    113.         public void open() throws SQLException
    114.    
    115.         {
    116.    
    117.             db = DBHelper.getWritableDatabase();
    118.    
    119.         }
    120.    
    121.      
    122.    
    123.          
    124.    
    125.         public void close()
    126.    
    127.         {
    128.    
    129.             DBHelper.close();
    130.    
    131.         }    
    132.    
    133.        
    134.    
    135.         public long AddUser(String username, String password)
    136.    
    137.         {
    138.    
    139.              ContentValues initialValues = new ContentValues();
    140.    
    141.              initialValues.put(KEY_USERNAME, username);
    142.    
    143.              initialValues.put(KEY_PASSWORD, password);      
    144.    
    145.              return db.insert(DATABASE_TABLE, null, initialValues);
    146.    
    147.      
    148.    
    149.         }
    150.    
    151.      
    152.    
    153.         public boolean Login(String username, String password) throws SQLException
    154.    
    155.         {
    156.    
    157.             Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
    158.    
    159.             if (mCursor != null) {          
    160.    
    161.                 if(mCursor.getCount() > 0)
    162.    
    163.                 {
    164.    
    165.                     return true;
    166.    
    167.                 }
    168.    
    169.             }
    170.    
    171.          return false;
    172.    
    173.         }
    174.    
    175.      
    176.    
    177.     }
    178.  

    XML
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2.  
    3. <RelativeLayout
    4.  
    5.  xmlns:android="http://schemas.android.com/apk/res/android"
    6.  
    7.  android:layout_width="fill_parent"
    8.  
    9.  android:layout_height="fill_parent" android:padding="10dip">
    10.  
    11.      <TextView
    12.  
    13.        android:id="@+id/lblUsername"
    14.  
    15.        android:layout_width="fill_parent"
    16.  
    17.        android:layout_height="wrap_content"      
    18.  
    19.        android:textSize="20sp" android:text="Username:"
    20.  
    21.        android:textStyle="bold"
    22.  
    23.        android:layout_marginBottom="8dip"
    24.  
    25.        />
    26.  
    27.       <EditText
    28.  
    29.                 android:id="@+id/txtUsername"
    30.  
    31.                 android:layout_below="@id/lblUsername"
    32.  
    33.                 android:layout_width="fill_parent"
    34.  
    35.                         android:layout_height="wrap_content"                  
    36.  
    37.                         android:textSize="12sp"
    38.  
    39.                         android:layout_marginBottom="8dip"
    40.  
    41.                 />
    42.  
    43.           <TextView
    44.  
    45.        android:id="@+id/lblPassword"
    46.  
    47.         android:layout_below="@id/txtUsername"
    48.  
    49.        android:layout_width="fill_parent"
    50.  
    51.        android:layout_height="wrap_content"      
    52.  
    53.        android:textSize="20sp" android:text="Password:"
    54.  
    55.        android:textStyle="bold"
    56.  
    57.        android:layout_marginBottom="8dip"
    58.  
    59.        />
    60.  
    61.       <EditText
    62.  
    63.                 android:id="@+id/txtPassword"
    64.  
    65.                 android:layout_below="@id/lblPassword"
    66.  
    67.                 android:layout_width="fill_parent"
    68.  
    69.                         android:layout_height="wrap_content"                  
    70.  
    71.                         android:textSize="12sp"
    72.  
    73.                         android:layout_marginBottom="8dip"
    74.  
    75.                        android:password="true"
    76.  
    77.                 />  
    78.  
    79.                
    80.  
    81.            <Button
    82.  
    83.                         android:id="@+id/btnLogin"
    84.  
    85.                         android:layout_below="@id/txtPassword"
    86.  
    87.                         android:layout_width="fill_parent"
    88.  
    89.                         android:layout_height="wrap_content"
    90.  
    91.                         android:text="Login"          
    92.  
    93.               />    
    94.  
    95. </RelativeLayout>
     

    Advertisement

  2. currycrab

    currycrab Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Sep 11, 2011

    Sep 11, 2011
    31
    0
    15
    programming student
    somewhere
  3. Laxidasical

    Laxidasical Newbie
    Rank:
    None
    Points:
    16
    Posts:
    25
    Joined:
    Aug 25, 2011

    Aug 25, 2011
    25
    3
    16
    Chicago
  4. currycrab

    currycrab Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Sep 11, 2011

    Sep 11, 2011
    31
    0
    15
    programming student
    somewhere
  5. Laxidasical

    Laxidasical Newbie
    Rank:
    None
    Points:
    16
    Posts:
    25
    Joined:
    Aug 25, 2011

    Aug 25, 2011
    25
    3
    16
    Chicago
    Code (Text):
    1.  
    If this is just a test app with a set username and password then I'd put something like the following in your onCreate method of your DatabaseHelper class:

    Code (Text):
    1.  
    2. String sql = "INSERT INTO " + DATABASE_TABLE + " (" + KEY_USERNAME + ", " + KEY_PASSWORD + ") VALUES ('myUsername', 'myPassword');"
    3. db.execSQL(sql);
    4.  
    If this is going to be in an app that requires users to enter their own username/password, then I'd use a dialog to get the data from users first and then enter it with the above code.

    Does that help?
     
  6. Laxidasical

    Laxidasical Newbie
    Rank:
    None
    Points:
    16
    Posts:
    25
    Joined:
    Aug 25, 2011

    Aug 25, 2011
    25
    3
    16
    Chicago
    Also, I just noticed you have a method called "AddUser" that basically does the same thing. Why not use that???
     
  7. currycrab

    currycrab Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Sep 11, 2011

    Sep 11, 2011
    31
    0
    15
    programming student
    somewhere
    because tis is not my code.

    i found this code online. and im a newbie in android programming.

    how do i add code under the add user part so that when i type the user name and password into the the textfield and when i press the login button it will check the username and passsword with the username andd password from the databse.
     
  8. Laxidasical

    Laxidasical Newbie
    Rank:
    None
    Points:
    16
    Posts:
    25
    Joined:
    Aug 25, 2011

    Aug 25, 2011
    25
    3
    16
    Chicago
    I just loaded the code and ran it. As I mentioned before, there are some things missing that you need to get this working. It sounds like you don't have a good grasp of Android/Java yet, so I would start by going through some general Android tutorials first. Just search Google for "getting started with Android" and you should find more than enough! Then look at the tutorials I sent you in my first post.

    To answer your question though. You'll have to create either a dialog or activity that asks the user to enter a new username & password. Then you'll insert the password into the DB and return the user back to the login screen. That can be done using the AddUser() method that's already in the code.

    If what I said it completely foreign to you then you REALLY need to study a bit more before diving into the code. I know that's probably not what you want to hear, but it will save you hours upon hours of troubleshooting later.
     
  9. currycrab

    currycrab Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Sep 11, 2011

    Sep 11, 2011
    31
    0
    15
    programming student
    somewhere

    hahas. okay. letme have a try first.

    actually what im doing is the username and password is already in the database jux when the user type in the username and password the programme will check if the username and password works.
     
  10. currycrab

    currycrab Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Sep 11, 2011

    Sep 11, 2011
    31
    0
    15
    programming student
    somewhere
    just download the sql browser and add the user's username and password and it worked.
     
  11. currycrab

    currycrab Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Sep 11, 2011

    Sep 11, 2011
    31
    0
    15
    programming student
    somewhere
    i jux realise that thou i change the database of my emulator it worked. when i put my app to the android phone. a new database is being created hence my users were all gone. if i were to hard code in my codes eariler on how do i do it???
     
  12. Laxidasical

    Laxidasical Newbie
    Rank:
    None
    Points:
    16
    Posts:
    25
    Joined:
    Aug 25, 2011

    Aug 25, 2011
    25
    3
    16
    Chicago
    Is this an app just for you or is this something you are posting in the market for all users? If this is going in the market (or you're handing it out to various users), you should pop a dialog asking the user for a username and password. Here are a couple of tutorials on how to make dialogs:



    If it's just for you, place this code at the very end of your onCreate method in your DatabaseHelper class (in db.java). NOTE: Be sure to edit your username and password!

    Code (Text):
    1. AddUser("[B][COLOR="Blue"]myUsername[/COLOR][/B]", "[B][COLOR="Blue"]myPassword[/COLOR][/B]");
     
    currycrab likes this.
  13. currycrab

    currycrab Member
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    31
    Joined:
    Sep 11, 2011

    Sep 11, 2011
    31
    0
    15
    programming student
    somewhere

    dude thanks. your code is very usefull. im doing the project for my self use. not for the market. thanks alot.
     
  14. Laxidasical

    Laxidasical Newbie
    Rank:
    None
    Points:
    16
    Posts:
    25
    Joined:
    Aug 25, 2011

    Aug 25, 2011
    25
    3
    16
    Chicago
    No problem, glad it worked out! Welcome to the wonderful world of Android development!
     

Share This Page

Loading...