2011-01-18 17 views

答えて

36

cursor.getColumnIndex(String columnName)は、列が存在しない場合は-1を返します。だから私は基本的に「XXXのLIMIT 0,1 SELECT * FROM」とカラムは、あなたが探している場合は、あなたが照会しようとすることができ

OR

が存在するかを決定するためにカーソルを使用するような単純なクエリを実行することになりコラム私は実際にはかなりきれいなようで、この機能を書いた「XXX FROM theColを選択」と例外

+0

私が考えたものだが、私は「そのようなコラムを言っていないSQLiteExceptionを取得していますそれ:テスト"私は、列がテーブルにあるかどうかをチェックしていない場合は、それを挿入します。 – Lunchbox

+0

クエリ中またはgetColumnIndex中に例外が発生していますか?クエリを実行する場合は、クエリでテストする列を指定しないでください(「SELECT FR FROM ...」ではなく「SELECT * FROM ..」を実行します)。それ以外の場合は、あなたが言及しているエラーをスローし、それをキャッチする必要があります。 – martinpelant

+0

これはクエリ中に発生します。 SQLiteQueryBuilderを使用してクエリを作成し、テーブルの列にまたがる投影マップを供給しています。 – Lunchbox

4

をキャッチ:

​​

私はこのようにそれを呼び出す:

@martinpelantsに基づい
29

My機能は答える:

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"); 
+6

'finally'ブロック内のカーソルを閉じる必要があります。 – WonderCsabo

3

は、フレキソのソリューションに少しを追加し、問題に私のソリューションです。

このメソッドは、任意のクラス(おそらく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(); 
    } 
} 
+1

これははるかにきれいに見えますが、私はいつもデータベースを閉じることをお勧めします。 –

+1

ええ、私はそれについてはわからなかったので、私はあなたがもはやそれを必要としない場合にのみ閉じるように言ったのです。 ActiveAndroidの – lwdthe1

2

あなたは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; 
    } 
0

を使用している場合、これは私のテストコードです:

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+","); 
+0

では、このコードでクラスのテーブル名を取得できます。 – Tom

+0

Cache.getTableInfo(type).getTableName() – Tom

関連する問題