我正在做注册活动来学习Room。在这里,创建了数据库,但未创建表。 感谢您的帮助。 当我打开UserDatabase时,Sqlite Manager chrome扩展程序将显示此内容。 从LoginDetails中选择*; 没有这样的表格:LoginDetails
这是我的“房间实体”模型课程 LoginDetails.java
...
这是我的SignupAcitvity.java
@Entity(tableName = "LoginDetails")
public class LoginDetails implements Serializable {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "Email")
private String email;
@ColumnInfo(name = "Password")
private String password;
public LoginDetails(String email, String password) {
this.email = email;
this.password = password;
}
public LoginDetails(){
}
@NonNull
public int getId() {
return id;
}
public void setId(@NonNull int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
这是我的LoginDao.java
SignupAcitvity.java
public class SignUpActivity extends AppCompatActivity {
private EditText edtEmail;
private EditText edtPassword;
private EditText edtConfirmPassword;
private Button btnRegister;
private LoginDao loginDao;
public static UserDatabase userDatabase;
private static final String TAG = "SignUpActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
edtEmail = findViewById(R.id.registerEmail);
edtPassword = findViewById(R.id.registerPassword);
edtConfirmPassword = findViewById(R.id.confirmPassword);
btnRegister = findViewById(R.id.registerButton);
userDatabase = Room.databaseBuilder(this, UserDatabase.class, "UserDatabase")
.allowMainThreadQueries()
.build();
loginDao = userDatabase.getLoginDao();
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!isEmpty()) {
if (android.util.Patterns.EMAIL_ADDRESS.matcher(edtEmail.getText().toString()).matches()) {
if (TextUtils.equals(edtPassword.getText().toString().trim(), edtConfirmPassword.getText().toString().trim())) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d(TAG, "email & password = " + edtEmail.getText().toString() + "," + edtPassword.getText().toString());
LoginDetails loginDetails = new LoginDetails(edtEmail.getText().toString(), edtPassword.getText().toString());
loginDao.insert(loginDetails);
Toast.makeText(getApplicationContext(), "User added Successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignUpActivity.this, MainActivity.class));
}
}, 1000);
} else {
edtConfirmPassword.setError("Password did not match");
}
} else {
edtEmail.setError("Invalid email");
}
} else {
Toast.makeText(SignUpActivity.this, "Empty Fields", Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean isEmpty() {
if (TextUtils.isEmpty(edtEmail.getText().toString()) ||
TextUtils.isEmpty(edtPassword.getText().toString()) ||
TextUtils.isEmpty(edtConfirmPassword.getText().toString())) {
return true;
} else {
return false;
}
}
}
这是我的UserDatabase.java
@Dao
public interface LoginDao {
@Query("Select * From LoginDetails Where Email Like :mail and Password Like :pwd")
LoginDetails findByMailPwd(String mail, String pwd);
@Query("Select * From LoginDetails")
List<LoginDetails> getAllLoginDetails();
@Insert
void insert(LoginDetails loginDetails);
@Update
void update(LoginDetails loginDetails);
@Delete
void delete(LoginDetails loginDetails);
}
答案 0 :(得分:0)
已解决。 代码没有问题。 我为Sqlite下载了DB Browser。它解决了我的问题。 谢谢大家和StackOverFlow。