Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello, I am new to sqlite and android. i m trying to develop an android app in which i have to do login. i had made a database but i am not confirmed that whether it is connected and giving the correct output of query or not?????? please help me..... i want to see the results of tables on the screen.
Java
public class MainActivity extends Activity {

   private EditText  username=null;
   private EditText  password=null;
   private TextView attempts;
   private Button login;
   int counter = 3;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      username = (EditText)findViewById(R.id.editText1);
      password = (EditText)findViewById(R.id.editText2);
      attempts = (TextView)findViewById(R.id.textView5);
      attempts.setText(Integer.toString(counter));
      login = (Button)findViewById(R.id.button1);
   
   
   public void login(View view){
    String[][] userPass = { { "user1", "pass1" },
               { "user2", "pass2" },
               { "user3", "pass3" } };

String value1 = "userToCheck";
String value2 = "passToCheck";

boolean userOk = false;
for (String[] up : userPass)
if (value1.equals(up[0]) && value2.equals(up[1]))
userOk = true;

System.out.println("User authenticated: " + userOk);
Toast.makeText(getApplicationContext(), "Redirecting...", 
       Toast.LENGTH_SHORT).show();

   }
   public void onLocationChanged(Location loc) {       
 username.setText("");
       username.setVisibility(View.INVISIBLE);
       Toast.makeText(
               getBaseContext(),
               "Location changed : Lat: " + loc.getLatitude() + " Lng: "
                       + loc.getLongitude(), Toast.LENGTH_SHORT).show();
       String longitude = "Longitude: " + loc.getLongitude();
       String TAG = null;
 Log.v(TAG, longitude);
       String latitude = "Latitude: " + loc.getLatitude();
       Log.v(TAG, latitude);

       /*----------to get City-Name from coordinates ------------- */
       String cityName = null;
       Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
       List<Address> addresses;
       try {
           addresses = gcd.getFromLocation(loc.getLatitude(),
                   loc.getLongitude(), 1);
           if (addresses.size() > 0)
               System.out.println(addresses.get(0).getLocality());
           cityName = addresses.get(0).getLocality();
       } catch (IOException e) {
           e.printStackTrace();
       }

       String s = longitude + "\n" + latitude + "\n\nMy Currrent City is: "
               + cityName;
       username.setText(s);
   }
   public void onProviderDisabled(String provider) {
       // TODO Auto-generated method stub
   }
   public void onProviderEnabled(String provider) {
       // TODO Auto-generated method stub
   }
   public void onStatusChanged(String provider, int status, Bundle extras) {
       // TODO Auto-generated method stub
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

}




Java
public class DBOperations {

	// Database fields
	private static DataBaseWrapper dbHelper;
	private String[] User_TABLE_COLUMNS = { DataBaseWrapper.username,DataBaseWrapper.userid, DataBaseWrapper.password};
	private SQLiteDatabase database;
	private static DBOperations operations = null;

	private DBOperations()
	{
	}
	public DBOperations(Context context) {
		dbHelper = new DataBaseWrapper(context);
	}
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}
	public void close() {
		dbHelper.close();
	}
	public static DBOperations getInstance(Context context)
	  {
		  if(operations == null)
		  {
			  dbHelper = new DataBaseWrapper(context);			  
			  operations = new DBOperations();
		  }
		  return operations;
	  }

public UserClass adduser(String number, String string) {

		ContentValues values = new ContentValues();

		values.put(DataBaseWrapper.username, string);
		values.put(DataBaseWrapper.password, number);

		long userid = database.insert(DataBaseWrapper.User, null, values);

		// now that the user is registered return it ...
		Cursor cursor = database.query(DataBaseWrapper.User,
				User_TABLE_COLUMNS, DataBaseWrapper.userid + " = "
						+ userid, null, null, null, null);

		cursor.moveToFirst();

		UserClass newuser = parseUser(cursor);
		cursor.close();
		return newuser;
		
		
	}




Java
public class DataBaseWrapper extends SQLiteOpenHelper {
//---------------------Table Users-------------------------
	public static final String User = "User";
	public static final String userid  = "userid";
	static final String username="username";
	static final String password="password";
	
	
	private static final String DATABASE_NAME = "fyp.db";
	protected static final int DATABASE_VERSION = 1;

private static final String UserCreate = "create table " + User
			+ "(" + userid + " integer primary key autoincrement, "
			+ username + " text not null);";
	
	
	public DataBaseWrapper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(UserCreate);
		}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// you should do some logging in here
		// ..

		db.execSQL("DROP TABLE IF EXISTS "+User);

		onCreate(db);
	}
}
Posted

1 solution

 
Share this answer
 
Comments
Member 10757865 29-Apr-14 1:51am    
Thanks for your response.
But please tell me how can i check that the values are entered in database when i enter it in from interface?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900