查询Android数据库是否存在! [英] Query if Android database exists!

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

问题描述

我为我的 android 应用程序创建了一个数据库,其中包含静态数据并且不需要更新/删除功能,因此当应用程序启动时,我想检查数据库是否存在,如果不存在,则执行我的 dbAdapter 类.我知道它是一个简单的 if 语句,但我只是想知道查询数据库是否存在的最有效方法.

I have created a database for my android app which contains static data and does not require update/delete functionality thus when the app starts, I want to check if the db exists and if not then execute my dbAdapter class. I know its a simple if statement but I was just wondering the most efficient way to query whether the db exists.

干杯

推荐答案

/**
 * Check if the database exist and can be read.
 * 
 * @return true if it exists and can be read, false if it doesn't
 */
private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH, null,
                SQLiteDatabase.OPEN_READONLY);
        checkDB.close();
    } catch (SQLiteException e) {
        // database doesn't exist yet.
    }
    return checkDB != null;
}

其中 DB_FULL_PATH 是数据库文件的路径.

where DB_FULL_PATH is the path to your database file.

我不只是检查文件是否存在的原因是因为它无法判断 (a) 它是否是一个 sqlite db 文件,(b) 该文件没有损坏并且实际上可以读取,即由于部分下载或者不管它是如何创建的.

And the reason I am not just checking if a file exists is because it would not tell whether (a) it's an sqlite db file, (b) the file is not corrupt and can actually be read, i.e. due to partial download or however it has been created.

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

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