0

I was trying few days to somehow successed creating the database and the table, but I just cant, and I need your help badly. My database is always empty, I dont even create the table. :/

Here are my classses:

ENTITY:

@Entity (tableName = "users")
class User {

    @ColumnInfo(name = "id")
    @PrimaryKey(autoGenerate = true)
    private
    long id;

    @ColumnInfo(name = "first_name")
    private
    String firstName;

    @ColumnInfo(name = "last_name")
    private
    String lastName;

    User(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }

    long getId() {
        return id;
    }

    String getFirstName() {
        return firstName;
    }

    String getLastName() {
        return lastName;
    }

    public void setId(long id) {
        this.id = id;
    }

}

DAO:

@Dao
public interface UserDao {

    @Insert
    long insertData(User user);

    @Update
    void updateData(User user);

    @Delete
    void deleteData(User user);
}

DATABASE:

@Database(entities = {User.class}, version = 1, exportSchema = false)
public abstract class UserDatabase extends RoomDatabase {
    public abstract UserDao userDao();
}

REPOSITORY:

class UserRepository {

    private Context context;
    private String DB_NAME = "userdb.db";
    private UserDatabase userDatabase;

    UserRepository(Context context){
        this.context = context;
        userDatabase = Room.databaseBuilder(context, UserDatabase.class, DB_NAME).build();

        Toast.makeText(context, "Database created...", Toast.LENGTH_SHORT).show();
    }

    @SuppressLint("StaticFieldLeak")
    void insertTask(User user){
        new AsyncTask<Void, Void, Void>(){
            @Override
            protected Void doInBackground(Void... voids) {
                long result = userDatabase.userDao().insertData(user);
                Log.d("LOG", String.valueOf(result));
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show();
            }
        }.execute();
    }
}

MAINACTIVITY (I'm getting the text from two edittext and passing in to Entity class)

public class MainActivity extends AppCompatActivity {

    EditText first_Name, last_Name;
    Button save_btn;

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

        first_Name = findViewById(R.id.first_name);
        last_Name = findViewById(R.id.last_name);
        save_btn = findViewById(R.id.save_btn);
        save_btn.setOnClickListener(view -> {
            UserRepository userRepository = new UserRepository(MainActivity.this);
            User user = new User(first_Name.getText().toString().trim(),
                    last_Name.getText().toString().trim());
            userRepository.insertTask(user);
        });
    }
3
  • Did you check you logcat? Is there any stack trace about that? Commented Jan 25, 2020 at 20:08
  • This may not be an issue, but your repository is not a singleton and your databaseBuilder does not use an application context. Commented Jan 25, 2020 at 20:39
  • make sure there is no exception being thrown in your AsycnTask . try using try catch in your doInBackground() method. Commented Jan 25, 2020 at 20:59

1 Answer 1

4

I dont even create the table

It known working Toast seen log having D/LOG: 1

You copy database (only userdb.db file) and using tool (like DB SQLite Browser) see database empty.

Needing to copy userdb.db and userdb.db-wal to seen in tool.

-wal having changes not checkpointed

Sign up to request clarification or add additional context in comments.

2 Comments

wow I did not know that. I have exported twose two files and now it works! Thanks bro you are a lifesaver! :)
@StefanJo could you tick the question answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.