Room在哪里保存Android数据库? [英] Where Room save database Android?

查看:1730
本文介绍了Room在哪里保存Android数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Room库将数据保存在数据库中.我想获取数据库.

I am using Room library to save data in database.i want to get database.

使用此代码

  private void copyFile() {

        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();

            if (sd.canWrite()) {
                String currentDBPath=getDatabasePath("photex_db.db").getAbsolutePath();
                String backupDBPath = "photex_db.db";
                File currentDB = new File(data, currentDBPath);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

它可以在简单的sqlLite中使用,但不适用于ROOM库房间

it works in simple sqlLite but did not work for ROOM Library ROOM

有什么办法可以获取数据库?

is there any way can get Database?

在Room的帮助下创建数据库的类

Class to create DataBase with help of Room

  @Database(entities = {ProjectDataEntity.class, SavedProjectEntity.class},
        version = 2)
     @TypeConverters(DateConverter.class)

     public abstract class AppDatabase extends RoomDatabase {

     static final String DATABASE_NAME = "photex_db";

     private static AppDatabase Instance;

     public abstract ProjectDataDao projectDataDao();

     public abstract SavedProjectDao savedProjectDao();

     public static AppDatabase getInstance(Context context) {
        if (Instance == null) {
            synchronized (AppDatabase.class) {
                if (Instance == null) {
                    Instance = 
      Room.databaseBuilder(context.getApplicationContext(),
                            AppDatabase.class, DATABASE_NAME)
                            .build();
                }
            }
        }
        return Instance;
    }


}

推荐答案

static final String DATABASE_NAME = "photex_db";

在这里,您正在尝试打开photoex_db.

Here, you are trying to open photoex_db.

String currentDBPath=getDatabasePath("photex_db.db").getAbsolutePath();

在这里,您正在尝试阅读photex_db.db.

Here, you are trying to read from photex_db.db.

这些不一样.

您可能会考虑一致地使用DATABASE_NAME,而不是仅在某些地方使用.

You might consider using DATABASE_NAME consistently, rather than only in some places.

这篇关于Room在哪里保存Android数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆