I am working on a game at the moment and all I want is my login screen to be able to check that the username and password I have already stored in my database is correct an
You can try below way.
Create class called DBAdapter.java and add following code.
public class DBAdapter {
private static final String DATABASE_NAME = "Your database name";
private static final String DATABASE_CREATE_USERS = "create table TABLE_NAME (
UserID integer primary key autoincrement, " + "Username text, Password text);" ;
private static final String DATABASE_SELECT_USERS = "users";
public static final String USER_ID = "UserID";
public static final String USER_NAME = "Username";
public static final String USER_PASSWORD = "Password ";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
DBAdapter(Contex ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
System.out.println("Creating table");
db.execSQL(DATABASE_CREATE_USERS);
}
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public Cursor fetchUser(String username, String password) {
Cursor myCursor = db.query(DATABASE_SELECT_USERS,
new String[] { USER_ID, USER_NAME, USER_PASSWORD },
USER_NAME + "='" + username + "' AND " +
USER_PASSWORD + "='" + password + "'", null, null, null, null);
if (myCursor != null) {
myCursor.moveToFirst();
}
return myCursor;
}
public void InsertData(String username, String password) {
String sql = "INSERT INTO users (Username,Password) VALUES('"+username+"','"+password+"')";
db.execSQL(sql);
}
}
and then create your Java file
Login.java
public class Login extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText txt_username = (EditText) findViewById(R.id.editText1);
final EditText txt_psw = (EditText) findViewById(R.id.editText2);
Button btn_signin = (Button) findViewById(R.id.button1);
// Inner class to implement Button Listener when button is clicked.
btn_signin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DBAdapter db = new DBAdapter(getBaseContext());
db.open();
db.InsertData("Chao", "1234");
Log.v("LoginDetails", txt_username.getText().toString()+"../.."
+txt_psw.getText().toString());
// Accessing user using the fetchUser method we created in DBAdapter
Cursor cur = db.fetchUser( txt_username.getText().toString(),
txt_psw.getText().toString());
// Use this line if you want to see the number of users with these login details
System.out.println("cur.getCount() "+cur.getCount());
if(cur.getCount()!=0) {
String usn=cur.getString(1);
if(usn.equals("Chao")) {
}
else {
}
System.out.println("Query succedded");
Toast.makeText(getBaseContext(), "Success! Valid User name and password", Toast.LENGTH_LONG).show();
db.close();
}
else
Toast.makeText(getBaseContext(), "Please enter Valid User name and password", Toast.LENGTH_LONG).show();
} //Closes the onClick method
}); //Closes the onClickListener
}
}