/**
* 判断某表里某字段是否存在
*
* @param db
* @param tableName
* @param fieldName
* @return
*/
public boolean isFieldExist(SQLiteDatabase db, String tableName, String fieldName) {
String queryStr = "select sql from sqlite_master where type = 'table' and name = '%s'";
queryStr = String.format(queryStr, tableName);
Cursor c = db.rawQuery(queryStr, null);
String tableCreateSql = null;
try {
if (c != null && c.moveToFirst()) {
tableCreateSql = c.getString(c.getColumnIndex("sql"));
}
} finally {
if (c != null)
c.close();
}
if (tableCreateSql != null && tableCreateSql.contains(fieldName))
return true;
return false;
}
/**更新table_name表 增加field_name字段**/
if (!dbHelper.isFieldExist(db, "table_name", "field_name")) {
db.execSQL("ALTER TABLE table_name ADD COLUMN field_name varchar(50)");
}
网友评论