如何检查Android中是否存在数据库? [英] How to check if a DB exists in Android?

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

问题描述

我正在使用Room API在我的Android应用中实现数据库。似乎每次我加载我的应用程序时,它都会尝试一次又一次地创建数据库。有什么方法可以限制这一点?

I am using Room API to implement a DB in my Android app. It seems that every time I load my app it tries to create the database again and again. Is there any way to restrict this?

 db = Room.databaseBuilder(context, AppDatabase.class, "database-name").build();


推荐答案

创建数据库时,在应用程序启动时调用他们的数据库create.you曾经在应用程序活动中使用下面的代码,而活动调用在应用程序类调用中的清单文件中,如下面的代码所示。.

when you create database it call when application start that time their db create.you used to below code in app activity and that activity call in manifest file in application class call like used below code ..

public class AppActivity extends Application {

AppDatabase db;

@Override
public void onCreate() {
    super.onCreate();
    db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build();
}

public AppDatabase getDatabase() {
    return db;
}

}
并在清单文件下方添加..在应用程序标记中

} and add below line manifest file .. add below line in application tag

        android:name="AppActivity"

这篇关于如何检查Android中是否存在数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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