检查Android中的应用程序数据库中是否存在列

在Android中有没有一种很好的方法来查看应用程序数据库中的表是否存在列? (我知道有类似这个问题,但似乎没有任何Android的具体。)

Solutions Collecting From Web of "检查Android中的应用程序数据库中是否存在列"

如果列不存在,则cursor.getColumnIndex(String columnName)返回-1。 所以我会基本上执行一个简单的查询,如“SELECT * FROM xxx LIMIT 0,1”,并使用光标来确定您正在寻找的列是否存在

要么

您可以尝试查询“SELECT theCol FROM xxx”列并捕获exception

我的基于@martinpelants的函数的答案是:

 private boolean existsColumnInTable(SQLiteDatabase inDatabase, String inTable, String columnToCheck) { Cursor mCursor = null; try { // Query 1 row mCursor = inDatabase.rawQuery("SELECT * FROM " + inTable + " LIMIT 0", null); // getColumnIndex() gives us the index (0 to ...) of the column - otherwise we get a -1 if (mCursor.getColumnIndex(columnToCheck) != -1) return true; else return false; } catch (Exception Exp) { // Something went wrong. Missing the database? The table? Log.d("... - existsColumnInTable", "When checking whether a column exists in the table, an error occurred: " + Exp.getMessage()); return false; } finally { if (mCursor != null) mCursor.close(); } } 

只需致电:

 boolean bla = existsColumnInTable(myDB,"MyTable","myColumn2check"); 

我其实写了这个看起来很干净的函数:

 private boolean field_exists( String p_query ) { Cursor mCursor = mDb.rawQuery( p_query, null ); if ( ( mCursor != null ) && ( mCursor.moveToFirst()) ) { mCursor.close(); return true ; } mCursor.close(); return false ; } 

我这样称呼它:

 if ( field_exists( "select * from sqlite_master " + "where name = 'mytable' and sql like '%myfield%' " )) { do_something ; } 

这是我对这个问题的解决方法,它增加了一些柔印的解决scheme。

你可以把这个方法放在任何类中,也许你的SQLiteOpenHelper扩展类。

 public static boolean columnExistsInTable(SQLiteDatabase db, String table, String columnToCheck) { Cursor cursor = null; try { //query a row. don't acquire db lock cursor = db.rawQuery("SELECT * FROM " + table + " LIMIT 0", null); // getColumnIndex() will return the index of the column //in the table if it exists, otherwise it will return -1 if (cursor.getColumnIndex(columnToCheck) != -1) { //great, the column exists return true; }else { //sorry, the column does not exist return false; } } catch (SQLiteException Exp) { //Something went wrong with SQLite. //If the table exists and your query was good, //the problem is likely that the column doesn't exist in the table. return false; } finally { //close the db if you no longer need it if (db != null) db.close(); //close the cursor if (cursor != null) cursor.close(); } } 

如果你使用ActiveAndroid

 public static boolean createIfNeedColumn(Class<? extends Model> type, String column) { boolean isFound = false; TableInfo tableInfo = new TableInfo(type); Collection<Field> columns = tableInfo.getFields(); for (Field f : columns) { if (column.equals(f.getName())) { isFound = true; break; } } if (!isFound) { ActiveAndroid.execSQL("ALTER TABLE " + tableInfo.getTableName() + " ADD COLUMN " + column + " TEXT;"); } return isFound; } 

这是我的testing代码:

 String neadle = "id"; //searched field name String tableName = "TableName"; boolean found = false; SQLiteDatabase mDb = ActiveAndroid.getDatabase(); Cursor mCursor = mDb.rawQuery( "SELECT * FROM sqlite_master WHERE name = '"+tableName+"' and sql like '%"+neadle+"%'" , null); mCursor.moveToFirst(); String fie = ","; if (mCursor.getCount() > 0) { String[] fields = mCursor.getString(mCursor.getColumnIndex("sql")).split(","); for (String field: fields) { String[] fieldNameType = field.trim().split(" "); if (fieldNameType.length > 0){ fie += fieldNameType[0]+","; } } }else { //table not exist! } if (mCursor != null) mCursor.close(); // return result: found = fie.contains(","+neadle+",");