如何从Android中的SQLite数据库获取数据

时间:2016-06-09 17:55:17

标签: java android sqlite

我想从数据库中获取我的登录活动的数据电子邮件和密码。

LoginActivity.java

public class LoginActivity extends AppCompatActivity {

Button loginButton;
EditText password;
EditText email;

DBAdapter db;

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

    db = new DBAdapter(this);
    db=db.open();

    this.email = (EditText) findViewById(R.id.editText);
    this.password = (EditText) findViewById(R.id.editText2);
    this.loginButton = (Button) findViewById(R.id.button);
    if (getIntent().getExtras() != null) {
        Toast.makeText(this, getIntent().getExtras().getString("toastMessage"), Toast.LENGTH_LONG).show();
    }

    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String s2 = password.getText().toString();

            String StoredPassword=db.getContacts1(s2);

            if (s2.equals(StoredPassword)) {
                Intent intent = new Intent(LoginActivity.this, PatientActivity.class);
                Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT).show();
                startActivity(intent);
            } else {
                Toast.makeText(getApplicationContext(), "Wrong Credentials", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

}

DBAdapter.java

public class DBAdapter {

public static final String KEY_NAME="Name";
public static final String KEY_EMAIL="Email";
public static final String KEY_PASSWORD="Password";
public static final String KEY_PHONE= "Phone";
public static final String KEY_ADDRESS="Address";

public static final String TAG="DBAdapter";
public static final String DATABASE_NAME="MyDB.db";
public static final String DATABASE_TABLE="Contacts";
public static final int DATABASE_VERSION=1;

public static final String SELECT_EMAIL = "SELECT Email FROM Contacts";
public static final String SELECT_PASSWORD = "SELECT Password FROM Contacts";

public static final String DATABASE_CREATE="CREATE TABLE Contacts(Name text primary key not null,Email text not null,Password text not null,Phone integer not null,Address text);";

public Context context;
public DatabaseHelper DBHelper;
public SQLiteDatabase db;

public DBAdapter(Context context)
{
    this.context = context;
    DBHelper = new DatabaseHelper(context);
}


private static class DatabaseHelper extends SQLiteOpenHelper {

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


    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(DATABASE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading Database from " + oldVersion + " to " + newVersion + " which will destroy all Old Data");
        db.execSQL("DROP TABLE IF EXISTS Contacts");
        onCreate(db);
    }

}

public DBAdapter open() throws SQLException
{
    db = DBHelper.getReadableDatabase();
    return this;
}

public void close()
{
    DBHelper.close();
}

public long insertContacts(String Name, String Email, String Password, String Phone, String Address)
{
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_NAME,Name);
    contentValues.put(KEY_EMAIL,Email);
    contentValues.put(KEY_PASSWORD,Password);
    contentValues.put(KEY_PHONE,Phone);
    contentValues.put(KEY_ADDRESS,Address);

    return db.insert(DATABASE_TABLE,null,contentValues);
}

public boolean deleteContacts(String Name)
{
    return db.delete(DATABASE_TABLE,KEY_NAME + " "+ Name,null)>0;
}

public Cursor getAllContacts()
{
    return db.query(DATABASE_TABLE,new String[] {KEY_NAME,KEY_EMAIL,KEY_PASSWORD,KEY_PHONE,KEY_ADDRESS},null,null,null,null,null);
}

public String getContacts(String Email) {

    Cursor cursor = db.query("DATABASE_TABLE", null, "KEY_EMAIL", new String[]{Email}, null, null, null, null);

    if (cursor == null) {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String Email1 = cursor.getString(cursor.getColumnIndex("KEY_EMAIL"));
    return Email1;
}

public String getContacts1(String Password)
{

    Cursor cursor = db.query("DATABASE_TABLE",null,"KEY_PASSWORD",new String[]{Password},null,null,null,null);

    if(cursor == null) {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String Password1 = cursor.getString(cursor.getColumnIndex("KEY_PASSWORD"));


    return Password1;
}

public boolean updateContacts(String Name,String Email,String Password,String Phone,String Address) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(KEY_NAME, Name);
    contentValues.put(KEY_EMAIL, Email);
    contentValues.put(KEY_PASSWORD, Password);
    contentValues.put(KEY_PHONE, Phone);
    contentValues.put(KEY_ADDRESS, Address);

    return db.update(DATABASE_TABLE, contentValues, KEY_NAME + " " + Name, null) > 0;
}

}

activity_login.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="mydoctor.code.in.mydoctor.LoginActivity">

<TextView
    android:text="Email:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="12dp"
    android:layout_marginStart="12dp"
    android:layout_marginTop="62dp"
    android:id="@+id/textView3" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/textView3"
    android:layout_toEndOf="@+id/textView3"
    android:layout_marginLeft="44dp"
    android:layout_marginStart="44dp"
    android:layout_marginTop="47dp"
    android:id="@+id/editText" />

<TextView
    android:text="Password:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText"
    android:layout_alignLeft="@+id/textView3"
    android:layout_alignStart="@+id/textView3"
    android:layout_marginTop="78dp"
    android:id="@+id/textView4" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:layout_below="@+id/editText"
    android:layout_alignLeft="@+id/editText"
    android:layout_alignStart="@+id/editText"
    android:layout_marginTop="63dp"
    android:id="@+id/editText2" />

<Button
    android:text="Login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="101dp"
    android:id="@+id/button"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

请帮助解决问题。我在代码中遇到错误。我的数据库已成功创建,但无法获取数据。

0 个答案:

没有答案