I want to check if the Username&Password that the user entered exists in the database I have tried this code, but i don't get it how to work with the cursor and what I'm doing wrong.
package com.example.nir.nestleapp;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class LoginActivity extends AppCompatActivity {
MyDBHandler Newdb;
private SQLiteDatabase _database = null;
private MyDBHandler _dbHelper = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Newdb = new MyDBHandler(this);
_dbHelper = new MyDBHandler(getApplicationContext());
_database = this.openOrCreateDatabase(MyDBHandler.DATABASE_NAME, MODE_PRIVATE, null);
SQLiteDatabase database=_dbHelper.getReadableDatabase();
final ArrayList list = new ArrayList();
final TextView RegisterPage=(TextView)findViewById(R.id.textView4);
TextView Text1=(TextView)findViewById(R.id.textView3);
Button Login=(Button)findViewById(R.id.LoginBtn);
Button GuestLogin=(Button)findViewById(R.id.LoginGuestBtn);
final EditText LoginUser=(EditText)findViewById(R.id.LoginUser);
final EditText LoginPass=(EditText)findViewById(R.id.LoginPassword);
RegisterPage.setTextSize(17);
Text1.setTextSize(17);
RegisterPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
}
});
GuestLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,MainPageActivity.class));
}
});
Cursor c = database.rawQuery("SELECT * FROM " + MyDBHandler.Table_Name, null);
if (c == null) return;
if (c.moveToFirst()) {
while ( !c.isAfterLast() ) {
list.add(c.getString(0));
list.add(c.getString(1));
c.moveToNext();
}
}
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(LoginUser.getText().toString().isEmpty()||LoginPass.getText().toString().isEmpty())
{
Toast.makeText(LoginActivity.this, "", Toast.LENGTH_SHORT).show();
}
if(list.contains(LoginUser.getText().toString())&&list.contains(LoginPass.getText().toString()))
{
Toast.makeText(LoginActivity.this, "!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, MainPageActivity.class));
}
else
{
Toast.makeText(LoginActivity.this, "wrong", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Here is my DBHelper:
package com.example.nir.nestleapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "UsersTable.db";
public static final String Table_Name = "UsersTable";
public static final String KEY_User = "User";
public static final String KEY_Password = "Password";
public static final String KEY_FullName = "FullName";
public static final String KEY_PhoneNumber="PhoneNumber";
public static final String KEY_IDNUMBER="IDNumber";
public static final String[] DB_COL = new String[]{KEY_User,KEY_Password,KEY_FullName,KEY_PhoneNumber,KEY_IDNUMBER};
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CreateTableSql = "Create Table " + Table_Name + " ( " +
KEY_User + " INTEGER PRIMARY KEY AUTOINCREMENT , " +
KEY_Password + " TEXT , " +
KEY_FullName + " TEXT , "+
KEY_PhoneNumber+" TEXT , "+
KEY_IDNUMBER+ " ) ";
db.execSQL(CreateTableSql);
}@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Table_Name);
onCreate(db);
}
public void Add(UserTable NewUser) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_User,NewUser.GetUserName() );
values.put(KEY_Password, NewUser.GetPassword());
values.put(KEY_FullName,NewUser.GetFullName());
values.put(KEY_PhoneNumber,NewUser.GetPhoneNumber());
values.put(KEY_IDNUMBER,NewUser.GetID());
db.insert(Table_Name, null, values);
db.close();
}
}
Updated LoginActivity
package com.example.nir.nestleapp;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
public class LoginActivity extends AppCompatActivity {
MyDBHandler Newdb;
private SQLiteDatabase _database = null;
private MyDBHandler _dbHelper = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Newdb = new MyDBHandler(this);
_dbHelper = new MyDBHandler(getApplicationContext());
_database = this.openOrCreateDatabase(MyDBHandler.DATABASE_NAME, MODE_PRIVATE, null);
final TextView RegisterPage=(TextView)findViewById(R.id.textView4);
TextView Text1=(TextView)findViewById(R.id.textView3);
Button Login=(Button)findViewById(R.id.LoginBtn);
Button GuestLogin=(Button)findViewById(R.id.LoginGuestBtn);
final EditText LoginUser=(EditText)findViewById(R.id.LoginUser);
final EditText LoginPass=(EditText)findViewById(R.id.LoginPassword);
RegisterPage.setTextSize(17);
Text1.setTextSize(17);
RegisterPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
}
});
GuestLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this,MainPageActivity.class));
}
});
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(LoginUser.getText().toString().isEmpty()||LoginPass.getText().toString().isEmpty())
{
Toast.makeText(LoginActivity.this, "!", Toast.LENGTH_SHORT).show();
}
if(_dbHelper.getUserRow(LoginUser.getText().toString(),LoginPass.getText().toString())>0)
{
Toast.makeText(LoginActivity.this, "good!", Toast.LENGTH_SHORT).show();
HashMap<String,String> userInfo = _dbHelper.getUserInfo(LoginUser.getText().toString(),LoginPass.getText().toString());
Intent intent = new Intent(LoginActivity.this,MainPageActivity.class);
intent.putExtra("userInfo",userInfo);
startActivity(intent);
}
else
{
Toast.makeText(LoginActivity.this, "wrong", Toast.LENGTH_SHORT).show();
}
}
});
}
}
I think your problem is on your c.getString()
in your whole loop. The first index should be ID
and it might not be ID
as well so the proper way is to using
c.getString(c.getColumnIndex("yourkey"));
Another tips try to not use rawQuery unless you really got no option. You could easily just do a check something like this.
Put this code inside your handler. It might have some syntax error you could try to fix it.
public int getUserRow(String username,String password){
SQLiteDatabase db = this.getReadableDatabase();
String[] column = {"id"};
Cursor cursor = db.query(handler.TABLE_USER,column,"User = ? AND Password = ?",new String[]{username,password},null,null,null);
int row = cursor.getCount();
return row;
}
When you check for login just simply so you don't have actually get the all user from the database to check wether this user is actually exists.
if(_dbHelper.getUserRow(yourUsername,yourPassword)){
//Login here
}
One more thing try to keep all database related code inside database class which it is very good pratice and you won't get any confusion after looking back the code.
Updated:
Here is the code when you wan to get the login user info add it inside your database helper as well you need to edit the column to match your column
public HashMap<String,String> getUserInfo(String username,String password){
HashMap<String,String> user = new HashMap<String,String>();
SQLiteDatabase db = this.getReadableDatabase();
String[] column = {"usernam","password","rest","of","your","key"};
Cursor cursor = db.query(handler.TABLE_USER,column,"User = ? AND Password = ?",new String[]{username,password},null,null,null);
int usernameIndex = cursor.getColumnIndex("username");
int passwordIndex = cursor.getColumnIndex("password");
int restIndex = cursor.getColumnIndex("rest");
int ofIndex = cursor.getColumnIndex("of");
int keyIndex = cursor.getColumnIndex("key");
if(cursor.getCount() > 0){
user.put("username",cursor.getString(usernameIndex));
user.put("password",cursor.getString(passwordIndex));
user.put("username",cursor.getString(restIndex));
user.put("of",cursor.getString(ofIndex));
user.put("key",cursor.getString(keyIndex));
}
return user;
}
So in your if statement try to add this. This code basically help you get user data and pass all the data to your user info activity.
if(_dbHelper.getUserRow(yourUsername,yourPassword)){
HashMap<String,String> userInfo = _dbHelper.getUserInfo(username,password);
Intent intent = new Intent(yourContext,YourActivity.class);
intent.putExtra("userInfo",userInfo);
startActivity(intent);
}
So now you pass all the data to the other activity you could grab it inside your onCreate.
userInfo = (HashMap<String, String>) getIntent().getSerializableExtra("userInfo");
Log.d("user",userInfo.get("your key"));
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.