单击“登录”按钮值从SQLite检索数据无法替换

时间:2017-02-27 05:16:02

标签: android sqlite

我创建了2个帐户,是的,它会显示下一个屏幕的值,但我的问题是在退出我的第二个帐户后我尝试登录我创建的第一个帐户但显示的值仍然在我的第二个帐户信息中。我需要你的帮助。我是否需要创建数据库的新副本?

DatabaseHelper.java

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_PASS = "pass";
private static final String COLUMN_MOBILE = "mobile";
SQLiteDatabase db;

private static final String TABLE_CREATE = "create table contacts(id integer primary key not null ,"+
        "name text not null, email text not null, pass text not null, mobile text not null);";

public DatabaseHelper(Context context){
    super(context, DATABASE_NAME,null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db =db;
}

public void insertContact(Contact c){
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query = "select * from contacts";
    Cursor cursor = db.rawQuery(query,null);
    int count = cursor.getCount();


    values.put(COLUMN_ID,count);
    values.put(COLUMN_NAME,c.getName());
    values.put(COLUMN_EMAIL,c.getEmail());
    values.put(COLUMN_PASS, c.getPass());
    values.put(COLUMN_MOBILE, c.getMobile());

    db.insert(TABLE_NAME,null,values);
    db.close();
}

public List<Contact> getAllData() {
    List<Contact> contactList = new ArrayList<Contact>();
    // Select All Query
    String selectQuery = "select name, email, mobile from contacts";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setName((cursor.getString(0)));
            contact.setEmail(cursor.getString(1));
            contact.setMobile(cursor.getString(2));

            contactList.add(contact);
        } while (cursor.moveToNext());
    }
    db.close();
    return contactList;
}

public String searchPass(String email){
    db = this.getReadableDatabase();
    String query = "select email, pass from " + TABLE_NAME;
    Cursor cursor = db.rawQuery(query,null);
    String a, b;
    b = "not found";
    if(cursor.moveToFirst()){
        do{
            a = cursor.getString(0);

            if(a.equals(email)){
                b = cursor.getString(1);
                break;
            }
        }while(cursor.moveToNext());
    }
    return b;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
    db.execSQL(query);
    this.onCreate(db);
}

Profile.java

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profileview);

    TextView vName = (TextView) findViewById(R.id.tvName);
    TextView vEmail = (TextView) findViewById(R.id.tvEmail);
    TextView vMobile = (TextView) findViewById(R.id.tvMobile);
    db = new DatabaseHelper(this);

    try {
        List<Contact> contacts = db.getAllData();
        for (Contact cn : contacts) {
            String name = cn.getName();
            String email = cn.getEmail();
            String mobile = cn.getMobile();
            vName.setText(name);
            vEmail.setText(email);
            vMobile.setText(mobile);
            // follow by getEmail and getMobile
        }
    }catch (Exception e){
        Toast pass = Toast.makeText(Profile.this,e.toString(),Toast.LENGTH_LONG);
        pass.show();
    }

    Button btnOut = (Button) findViewById(R.id.btnOut);
    btnOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent Outent = new Intent(Profile.this,MainActivity.class);
            startActivity(Outent);
            finish();
        }
    });

}

Login.java

DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    Button login = (Button) findViewById(R.id.btnLogin);
    Button createAccount = (Button) findViewById(R.id.btnSignup);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText a = (EditText) findViewById(R.id.edEmail);
            String str = a.getText().toString();
            EditText b = (EditText) findViewById(R.id.edPassword);
            String pass = b.getText().toString();

            String password = helper.searchPass(str);
            if (pass.equals(password)) {

                Toast pasa = Toast.makeText(MainActivity.this,"Login Successful!", Toast.LENGTH_LONG);
                pasa.show();

                Intent i = new Intent(MainActivity.this, Profile.class);
                startActivity(i);
                finish();
            } else {
                Toast temp = Toast.makeText(MainActivity.this, "Email and password don't match!", Toast.LENGTH_LONG);
                temp.show();
            }
        }
    });

    createAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent newIntent = new Intent(MainActivity.this,SignUp.class);
            startActivity(newIntent);
            finish();
        }
    });

}

3 个答案:

答案 0 :(得分:0)

HI使用where条件显示登录帐户数据。请查看以下

//传递您用于登录的名称或电子邮件

 public List<Contact> getAllData_BY_NAMEOREMAIL(String name) {
        List<Contact> contactList = new ArrayList<Contact>();

       SQLiteDatabase db = this.getWritableDatabase();

         Cursor cursor = db.rawQuery("select name, email, mobile from contacts WHERE name = ?;",
                    new String[] { name });

       // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setName((cursor.getString(0)));
                contact.setEmail(cursor.getString(1));
                contact.setMobile(cursor.getString(2));

                contactList.add(contact);
            } while (cursor.moveToNext());
        }
        db.close();
        return contactList;
    }

答案 1 :(得分:0)

您的功能将使用ID

public Contact getContact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query("contacts", new String[] {"name", "email",
"mobile" }, "id" + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

Contact contact = new Contact();
            contact.setName((cursor.getString(0)));
            contact.setEmail(cursor.getString(1));
            contact.setMobile(cursor.getString(2));


    return contact;
}

答案 2 :(得分:0)

请使您的表架构基于Id的主键如下: 为帐户和插入数据创建2个ID,其中id = account id,并且还获取id = account id的数据。