gpt4 book ai didi

android - 表未在房间数据库中创建

转载 作者:行者123 更新时间:2023-11-30 04:56:24 24 4
gpt4 key购买 nike

我只是通过注册 Activity 来学习 Room。在这里,创建了数据库但未创建表。感谢您的帮助。当我打开 UserDatabase 时,Sqlite Manager chrome 扩展显示了这一点。从登录详细信息中选择 *;没有这样的表:LoginDetails

这是我的房间实体模型类 登录详情.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;
}
}

这是我的 SignupAcitvity.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;
}
}
}

这是我的 LoginDao.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);
}

这是我的 UserDatabase.java

@Database(entities = {LoginDetails.class}, version = 1, exportSchema = false)
public abstract class UserDatabase extends RoomDatabase {
public abstract LoginDao getLoginDao();

}

最佳答案

验证 Android SQLLite 数据库的最佳方法是使用 Android Studio 4.X(及更高版本)中提供的数据库检查器。

应用在 Debug模式下运行时尝试:

View > Tool Windows > App Inspection

然后在连接设备后从选项卡中选择数据库检查

关于android - 表未在房间数据库中创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59064677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com