1
我是Android的新手。所以我的應用程序需要用戶訪問,所以我已經設置了佈局文件。用戶註冊並登錄,存儲數據庫並使用SQLite查詢用戶名和密碼
我已經創建了數據庫模式,創建了一個擴展SQliteOpenHelper的類,分配了contentValues,並且我還進行了一次查詢以檢索用戶名和密碼。
記錄消息數據庫被創建,新的行也被插入到數據庫中,但是當我使用查詢在登錄屏幕中獲取用戶時,它的運行故障。
所以總結起來,我的查詢讓我感到困擾。
SQliteOpenHeler類
package com.example.andorid.ersnexus.database;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.andorid.ersnexus.Message;
import com.example.andorid.ersnexus.database.UserDbSchema.UserTable;
public class UserBaseHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
private static final String DATABASE_NAME = "userBase.db";
private SQLiteDatabase mDatabase;
private Context context;
public UserBaseHelper (Context context) {
super(context, DATABASE_NAME, null, VERSION);
this.context = context;
Message.message("Constructor Called", context);
}
@Override
public void onCreate (SQLiteDatabase db) {
db.execSQL("create table " + UserTable.NAME + "(" +
" _id integer primary key autoincrement, " +
UserTable.Cols.UUID + ", " +
UserTable.Cols.ENROLLMENT_NUMBER + ", " +
UserTable.Cols.USER_NAME + ", " +
UserTable.Cols.FULL_NAME + ", " +
UserTable.Cols.PASSWORD + ", " +
UserTable.Cols.EMAIL + "," +
UserTable.Cols.DATE_OF_BIRTH +
")"
);
Message.message("onCreate() called", context);
}
public String fetchUser (String userName) {
mDatabase = this.getReadableDatabase();
UserCursorWrapper c = queryCrimes(new String[]{UserTable.Cols.USER_NAME, UserTable.Cols.PASSWORD},
UserTable.Cols.USER_NAME + " = ?", new String[]{userName});
String uname,pass;
pass = "not found";
try {
c.moveToFirst();
while (!c.isAfterLast()) {
uname = c.getString(c.getColumnIndex(UserTable.Cols.USER_NAME));
if(uname.equals(userName)){
pass = c.getString(c.getColumnIndex(UserTable.Cols.PASSWORD));
}
c.moveToNext();
}
}finally{
c.close();
}
return pass;
}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS " + UserTable.NAME;
db.execSQL(query);
Message.message("Database Upgraded", context);
this.onCreate(db);
}
private UserCursorWrapper queryCrimes (String[] projection, String whereClause, String[] whereArgs) {
Cursor cursor = mDatabase.query(
UserTable.NAME,
projection,//Columns - null select columns
whereClause,
whereArgs,
null,//groupBy
null,//having
null //orderBy
);
return new UserCursorWrapper(cursor);
}
}
工作
當用戶點擊註冊活動提交按鈕的contentValues使用的方法ADDUSER(的UserData用戶數據)
public void addUser (UserData userData) {
ContentValues values = getContentValues(userData);
mDatabase.insert(UserTable.NAME, null, values);
Message.message("Row inserted",mContext);
}
public static ContentValues getContentValues (UserData userData) {
ContentValues values = new ContentValues();
values.put(UserTable.Cols.UUID, userData.getId().toString());
values.put(UserTable.Cols.ENROLLMENT_NUMBER, userData.getEnrollmentNumber());
values.put(UserTable.Cols.USER_NAME, userData.getUserName());
values.put(UserTable.Cols.FULL_NAME, userData.getFullName());
values.put(UserTable.Cols.PASSWORD, userData.getPassword());
values.put(UserTable.Cols.EMAIL, userData.getEmail());
values.put(UserTable.Cols.DATE_OF_BIRTH, userData.getDob().toString());
return values;
}
存儲
現在在用戶登錄登錄時的登錄活動中,但噸以下查詢使用
public String fetchUser (String userName) {
mDatabase = this.getReadableDatabase();
UserCursorWrapper c = queryCrimes(new String[]{UserTable.Cols.USER_NAME, UserTable.Cols.PASSWORD},
UserTable.Cols.USER_NAME + " = ?", new String[]{userName});
String uname,pass;
pass = "not found";
try {
c.moveToFirst();
while (!c.isAfterLast()) {
uname = c.getString(c.getColumnIndex(UserTable.Cols.USER_NAME));
if(uname.equals(userName)){
pass = c.getString(c.getColumnIndex(UserTable.Cols.PASSWORD));
}
c.moveToNext();
}
}finally{
c.close();
}
return pass;
}
private UserCursorWrapper queryCrimes (String[] projection, String whereClause, String[] whereArgs) {
Cursor cursor = mDatabase.query(
UserTable.NAME,
projection,//Columns - null select columns
whereClause,
whereArgs,
null,//groupBy
null,//having
null //orderBy
);
return new UserCursorWrapper(cursor);
}
這裏是什麼需要登錄活動得到fetchUser的用戶名(用戶名)方法
package com.example.andorid.ersnexus;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.andorid.ersnexus.database.UserBaseHelper;
public class UserLoginActivity extends AppCompatActivity {
private EditText mUserName;
private EditText mUserPassword;
private Button mLoginButton;
private Button mSignUpButton;
private UserBaseHelper mHelper;
private String userName;
private String pass;
private String password;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_user_login);
mHelper = new UserBaseHelper(this);
//user UserName editText in fragment_user_login
mUserName = (EditText) findViewById(R.id.login_user_name);
//PASSWORD editText
mUserPassword = (EditText) findViewById(R.id.login_user_pass);
//SignUp button
mSignUpButton = (Button) findViewById(R.id.sign_up_button);
mSignUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
Intent i = new Intent(UserLoginActivity.this, UserSignUpActivity.class);
startActivity(i);
}
});
//Login Button
mLoginButton = (Button) findViewById(R.id.login_button);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
userName = mUserName.getText().toString();
pass = mUserPassword.getText().toString();
password = mHelper.fetchUser(userName);
if (pass.equals(password)) {
Intent i = new Intent(UserLoginActivity.this, UserProfileActivity.class);
i.putExtra("username", userName);
startActivity(i);
}
else{
Toast.makeText(UserLoginActivity.this, "Wrong Password", Toast.LENGTH_SHORT).show();
TextView textView = (TextView) findViewById(R.id.test_textView);
textView.setText(password);
}
}
});
}
}
UserSignUpActivity
package com.example.andorid.ersnexus;
import android.app.Activity;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.andorid.ersnexus.database.UserBaseHelper;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
public class UserSignUpFragment extends Fragment {
private static final int REQUEST_DATE = 0;
private static final String DIALOG_DATE = "dialog_date";
private EditText mErNo1;
private EditText mErNo2;
private EditText mErNo3;
private EditText mErNo4;
private EditText mErNo5;
private EditText mErNo6;
private EditText mErNo7;
private EditText mErNo8;
private EditText mErNo9;
private EditText mErNo10;
private EditText mErNo11;
private EditText mErNo12;
private EditText mUserName;
private EditText mFullName;
private EditText mPassword;
private EditText mEmail;
private EditText mConfirmPassword;
private Button mDob;
private Button mSubmit;
private UserData mUserData;
private String erNo1;
private String erNo2;
private String erNo3;
private String erNo4;
private String erNo5;
private String erNo6;
private String erNo7;
private String erNo8;
private String erNo9;
private String erNo10;
private String erNo11;
private String erNo12;
private UserBaseHelper mHelper;
DateFormat formatDate = DateFormat.getDateInstance();
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserData = new UserData();
mHelper = new UserBaseHelper(getActivity());
}
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_user_sign_up, container, false);
//enrollmentNumber of user
mErNo1 = (EditText) v.findViewById(R.id.erno_1);
mErNo1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo1(s.toString());
if (mErNo1.getText().length() == 1) {
mErNo2.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo1 = mErNo1.getText().toString();
mErNo2 = (EditText) v.findViewById(R.id.erno_2);
mErNo2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo2(s.toString());
if (mErNo2.getText().length() == 1) {
mErNo3.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo2 = mErNo2.getText().toString();
mErNo3 = (EditText) v.findViewById(R.id.erno_3);
mErNo3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo3(s.toString());
if (mErNo3.getText().length() == 1) {
mErNo4.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo3 = mErNo3.getText().toString();
mErNo4 = (EditText) v.findViewById(R.id.erno_4);
mErNo4.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo4(s.toString());
if (mErNo4.getText().length() == 1) {
mErNo5.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo4 = mErNo4.getText().toString();
mErNo5 = (EditText) v.findViewById(R.id.erno_5);
mErNo5.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo5(s.toString());
if (mErNo5.getText().length() == 1) {
mErNo6.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo5 = mErNo5.getText().toString();
mErNo6 = (EditText) v.findViewById(R.id.erno_6);
mErNo6.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo6(s.toString());
if (mErNo6.getText().length() == 1) {
mErNo7.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo6 = mErNo6.getText().toString();
mErNo7 = (EditText) v.findViewById(R.id.erno_7);
mErNo7.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo7(s.toString());
if (mErNo7.getText().length() == 1) {
mErNo8.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo7 = mErNo7.getText().toString();
mErNo8 = (EditText) v.findViewById(R.id.erno_8);
mErNo8.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo8(s.toString());
if (mErNo8.getText().length() == 1) {
mErNo9.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo8 = mErNo8.getText().toString();
mErNo9 = (EditText) v.findViewById(R.id.erno_9);
mErNo9.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo9(s.toString());
if (mErNo9.getText().length() == 1) {
mErNo10.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo9 = mErNo9.getText().toString();
mErNo10 = (EditText) v.findViewById(R.id.erno_10);
mErNo10.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo10(s.toString());
if (mErNo10.getText().length() == 1) {
mErNo11.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo10 = mErNo10.getText().toString();
mErNo11 = (EditText) v.findViewById(R.id.erno_11);
mErNo11.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo11(s.toString());
if (mErNo11.getText().length() == 1) {
mErNo12.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo11 = mErNo11.getText().toString();
mErNo12 = (EditText) v.findViewById(R.id.erno_12);
mErNo12.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setErNo12(s.toString());
if (mErNo12.getText().length() == 1) {
mUserName.requestFocus();
}
}
@Override
public void afterTextChanged (Editable s) {
}
});
erNo12 = mErNo12.getText().toString();
//USER_NAME editText field in fragment_user_sign_up
mUserName = (EditText) v.findViewById(R.id.userName_editText);
mUserName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setUserName(s.toString());
}
@Override
public void afterTextChanged (Editable s) {
}
});
final String userName = mUserName.getText().toString();
//FULL_NAME editText field in fragment_user_sign_up
mFullName = (EditText) v.findViewById(R.id.fullName_editText);
mFullName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setFullName(s.toString());
}
@Override
public void afterTextChanged (Editable s) {
}
});
final String fullName = mFullName.getText().toString();
//PASSWORD editText field in fragment_user_sign_up
mPassword = (EditText) v.findViewById(R.id.passwd_editText);
mPassword.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setPassword(s.toString());
}
@Override
public void afterTextChanged (Editable s) {
}
});
final String pass = mPassword.getText().toString();
//confirmPassword editText field in fragment_user_sign_up
mConfirmPassword = (EditText) v.findViewById(R.id.confirm_passwd_editText);
mConfirmPassword.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setCorrectPassword(s.toString());
}
@Override
public void afterTextChanged (Editable s) {
}
});
final String cPass = mConfirmPassword.getText().toString();
//EMAIL Id of user
mEmail = (EditText)v.findViewById(R.id.emai_id_editText);
mEmail.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged (CharSequence s, int start, int before, int count) {
mUserData.setEmail(s.toString());
}
@Override
public void afterTextChanged (Editable s) {
}
});
//datOFBirth button
mDob = (Button) v.findViewById(R.id.dob_button);
updateDate();
mDob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
FragmentManager manager = getFragmentManager();
DatePickerFragment dialog = DatePickerFragment.newInstance(mUserData.getDob());
dialog.setTargetFragment(UserSignUpFragment.this, REQUEST_DATE);
dialog.show(manager, DIALOG_DATE);
}
});
//submit button
mSubmit = (Button) v.findViewById(R.id.submit_button);
mSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
if (mUserName.length() == 0) {
createToast("Enter UserName");
} else if (mFullName.length() == 0) {
createToast("Enter FullName");
} else if (mPassword.length() == 0) {
createToast("Enter Password");
} else if (!mUserData.getPassword().equals(mUserData.getCorrectPassword())) {
createToast("Password And Confirm PassWord does not match");
} else if (mErNo1.length() == 0 &&
mErNo2.length() == 0 &&
mErNo3.length() == 0 &&
mErNo4.length() == 0 &&
mErNo5.length() == 0 &&
mErNo6.length() == 0 &&
mErNo7.length() == 0 &&
mErNo8.length() == 0 &&
mErNo9.length() == 0 &&
mErNo10.length() == 0 &&
mErNo11.length() == 0 &&
mErNo12.length() == 0) {
createToast("Enter Enrollment Number");
}
else {
//Intent i = UserProfileActivity.newIntent(getActivity(), mUserData.getUserName(), mUserData.getFullName(), fetchEnrollmentNumber());
// startActivity(i);
try{
Message.message("Database Created",getActivity());
createNewUser();
}catch (SQLException e){
Toast.makeText(getActivity(), "DATABASE NOT SAVED", Toast.LENGTH_SHORT).show();
}
}
}
});
return v;
}
public void createToast (String toast) {
Toast.makeText(getActivity(), toast, Toast.LENGTH_SHORT).show();
}
public String fetchEnrollmentNumber() {
String ErNo = mUserData.getErNo1() +
mUserData.getErNo2() +
mUserData.getErNo3() +
mUserData.getErNo4() +
mUserData.getErNo5() +
mUserData.getErNo6() +
mUserData.getErNo7() +
mUserData.getErNo8() +
mUserData.getErNo9() +
mUserData.getErNo10() +
mUserData.getErNo11() +
mUserData.getErNo12();
mUserData.setEnrollmentNumber(ErNo);
return mUserData.getEnrollmentNumber();
}
@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_DATE) {
Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
mUserData.setDob(date);
updateDate();
}
}
private void updateDate() {
mDob.setText(formatDate.format(mUserData.getDob()));
}
public void createNewUser(){
mUserData.getUserName();
mUserData.getPassword();
UserLab.get(getActivity()).addUser(mUserData);
}
}
所以我會欣賞有些幫助或其他方式來完成這項工作。
還好爵士的範圍之外讓我知道 –
所以我已經照你說的做了,但仍然不起作用,我也按照你的要求編輯了我的帖子。我還在手機中標記了一件事onCreate數據庫被調用時我使用註冊活動文件中的提交按鈕,但在模擬器常量中SQLiteOpenHelper類的祖先方法被稱爲兩次。我不知道這是否有幫助,雖然 –
你確定行是否真的被插入?你的代碼沒有檢查它,它只記錄插入行的消息。如果您確定它們是,您可以在onClick(和數據庫操作)中記錄用戶名和密碼,以驗證數據是否符合您的期望。 –