2010-08-02 245 views
55

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

干杯

+0

此链接是有帮助的: http://sree.cc/google/android/checking-if-a-database-or-table-exists-in-an-android- sqlite – 2012-01-22 09:17:37

回答

73
/** 
* 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是通向你的数据库文件。

而我不只是检查文件是否存在是因为它不会告诉是否(a)它是一个sqlite数据库文件,(b)该文件没有损坏,并且实际上可以读取,即由于部分下载或者它已经被创建。

+4

我试着用这个来看看我是否必须从包中复制“starter”数据库,或者只是继续并加载那里。所以我试着用这个。但对我来说,即使没有数据库存在,它也会一直返回true。 – 2013-12-16 02:54:38

+3

-1使用try/catch执行正常的非例外操作 – eddi 2014-11-16 21:43:22

+2

使用try/catch来查看是否有空值是非常可怕的......为什么不'checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH,null , SQLiteDatabase。OPEN_READONLY);如果(checkDB == null){//数据库尚不存在}。我想这不是你的错,似乎java被这种废话所困扰... – Kevin 2015-04-07 09:26:50

4

当初始化以下类:

mOpenHelper = new DatabaseHelper(getContext()); 

,如果它不存在,它会自动创建数据库。 它还允许通过将DB_VER更改为更高的数字来升级数据库。

然后让你能够查询数据库的使用:

SQLiteDatabase db = mOpenHelper.getWritableDatabase(); 

上面让你db.query() & db.insert()等方法。

private static class DatabaseHelper extends SQLiteOpenHelper { 

    private static final String DB_NAME = "db_name.db"; 
    private static final int DB_VER = 1; 

    public DatabaseHelper(Context context) { 
     super(context, DB_NAME, null, DB_VER); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

     db.execSQL("CREATE TABLE table_name (" + "_id INTEGER PRIMARY KEY, " 
       + " column_name_2 TEXT);"); 


       .execSQL("INSERT INTO table_name " 
         + "(column_name_2) " 
         + "VALUES " + "('hello world');"); 


    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

     Log.w(TAG + "Upgrading database from version " + oldVersion + " to " 
       + newVersion + ", which will destroy all old data"); 
     try { 
      db.execSQL("DROP TABLE IF EXISTS table_name"); 
      onCreate(db); 

     } catch (SQLException e) { 
      Log.e(TAG + "getting exception " 
        + e.getLocalizedMessage().toString()); 
     } 
    } 

} 
2

很简单: 在try块中包含DA DATABSE的路径打开数据库:

try{ 
    SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/bangla.rana.fahim/databases/dictionary", null,0); 
      Log.d("opendb","EXIST"); 
      dbe.close(); 
      } 

如果出现异常,那么数据库不会退出,以便创建它:

catch(SQLiteException e){ 
       Log.d("opendb","NOT EXIST"); 

      SQLiteDatabase db = openOrCreateDatabase("dictionary", MODE_PRIVATE, null); 
        db.execSQL("CREATE TABLE IF NOT EXISTS LIST(wlist varchar);"); 

        db.execSQL("INSERT INTO LIST VALUES('খবর');"); 
        db.execSQL("INSERT INTO LIST VALUES('কবর');"); //whatever you want 
       db.close(); 
} 

就是这样你:)

139

我宁愿检查存在的文件直接:

private static boolean doesDatabaseExist(Context context, String dbName) { 
    File dbFile = context.getDatabasePath(dbName); 
    return dbFile.exists(); 
} 
+4

干净简洁!爱它。谢谢。 – 2012-10-24 06:25:21

+0

作为上下文,我应该发送什么? – madprops 2013-01-13 21:13:58

+1

任何上下文都应该做,甚至应用上下文 – rds 2013-01-13 22:58:52

0

在你的主要活动中创建一个全局数据库辅助类对象。在MainActivity的onCreate()函数试试这个:

//global database helper variable 

DatabaseHelper dbh; 

//in the onCreate() 

if(dbh.getReadableDatabase()!=null) 
//view the list 

else 
//create db 
+1

这不回答问题。它将与DatabaseHelper的代码一起使用。 – rds 2018-02-19 15:39:53

0

我试过的版本由马蒂亚斯Conradt提供,但我发现,简单地检查是否DB = null是不够的!我已经修改了这个:

  /** 
     * 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(String InDBFile) { 
      SQLiteDatabase checkDB = null; 
      boolean res = false; 
      try { 
       checkDB = SQLiteDatabase.openDatabase(InDBFile, null, 
         SQLiteDatabase.OPEN_READONLY); 
       res = (checkDB.getVersion() > 0); 
       checkDB.close(); 
      } catch (SQLiteException e) { 
       // database doesn't exist yet. 
       Log.e("checkDataBase", "Selected file could not be opened as DB."); 

       res = false; 
      } 
      return res; 
     }