使用getContentResolver()。update没有这样的列Exception

时间:2015-06-10 19:27:09

标签: java android sqlite

我有以下方法只更新数据库中现有记录“tag”的COL_COUNT(最初设置为1):

public void addCount(){
     mFtag = "tag";
     String t = "2";
     ContentValues values = new ContentValues();
     values.put(FTagsContentProvider.COL_COUNT, t );
     getContentResolver().update(FTagsContentProvider.CONTENT_URI, values, "COL_TEXT=?", new String[]{mFtag});
              }

当我运行它时,我得到以下异常: enter image description here

我尝试更改行

getContentResolver().update(FTagsContentProvider.CONTENT_URI, values, "COL_TEXT=?", new String[]{mFtag});

getContentResolver().update(FTagsContentProvider.CONTENT_URI, values, FTagsContentProvider.COL_TEXT + "=?", new String[]{mFtag});

仍然相同的例外只是从“没有这样的列:COL_TEXT ”更改为“没有这样的列:标记

以下是ContentProvider:

public class FTagsContentProvider extends ContentProvider {

     static final String AUTHORITY = "ch.ethz.twimight.FTags";
     private static final String BASE_PATH = "ftags";
     public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
              + "/" + BASE_PATH);

     // fields for the database
     public static final String COL_ID = "id";
     public static final String COL_TEXT = "text";
     public static final String COL_COUNT = "count";


     static final int FTAGS = 1;
     static final int FTAGS_ID = 2;

     DBHelper dbHelper;


     private static HashMap<String, String> FTagsMap;

     static final UriMatcher uriMatcher;
       static{
          uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
          uriMatcher.addURI(AUTHORITY, BASE_PATH, FTAGS);
          uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", FTAGS_ID);

       }

       // database declarations
       private SQLiteDatabase database;
       static final String DATABASE_NAME = "ftagtable.db";
       static final String TABLE_FTAGS = "FTags";
       static final int DATABASE_VERSION = 1;
       private static final String TABLE_FTAGS_CREATE = "create table " 
                  + TABLE_FTAGS
                  + "(" 
                  + COL_ID + " integer primary key autoincrement, " 
                  + COL_TEXT + " text not null, " 
                  + COL_COUNT + " integer"
                  + ");";

       private static class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
             db.execSQL(TABLE_FTAGS_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            Log.w(DBHelper.class.getName(),
                    "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ". Old data will be destroyed");
            db.execSQL("DROP TABLE IF EXISTS " +  TABLE_FTAGS);
            onCreate(db);
        }

       }

    @Override
    public boolean onCreate() {
        // TODO Auto-generated method stub
        Context context = getContext();
        dbHelper = new DBHelper(context);
        database = dbHelper.getWritableDatabase();

        if(database == null)
            return false;
        else
            return true;    
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
         SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

         queryBuilder.setTables(TABLE_FTAGS);

          switch (uriMatcher.match(uri)) {

          case FTAGS:
              queryBuilder.setProjectionMap(FTagsMap);
             break;
          case FTAGS_ID:
              queryBuilder.appendWhere( COL_ID + "=" + uri.getLastPathSegment());
             break;

          default:
             throw new IllegalArgumentException("Unknown URI " + uri);
          }
          if (sortOrder == null || sortOrder == ""){

             sortOrder = COL_TEXT;
          }
          Cursor cursor = queryBuilder.query(database, projection, selection, 
                  selectionArgs, null, null, sortOrder);
          /** 
           * register to watch a content URI for changes
           */
          cursor.setNotificationUri(getContext().getContentResolver(), uri);

          return cursor;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO Auto-generated method stub
        long row = database.insert(TABLE_FTAGS, "", values);


          if(row > 0) {
             Uri newUri = ContentUris.withAppendedId(CONTENT_URI, row);
             getContext().getContentResolver().notifyChange(newUri, null);
             return newUri;
          }
          throw new SQLException("Fail to add a new record into " + uri);
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        // TODO Auto-generated method stub
         int rowsUpdated = 0;

          switch (uriMatcher.match(uri)){
          case FTAGS:
             rowsUpdated = database.update(TABLE_FTAGS, values, selection, selectionArgs);
             break;
          case FTAGS_ID:
             rowsUpdated = database.update(TABLE_FTAGS, values, COL_ID + 
                     " = " + uri.getLastPathSegment() + 
                     (!TextUtils.isEmpty(selection) ? " AND (" +
                     selection + ')' : ""), selectionArgs);
             break;

          default: 
             throw new IllegalArgumentException("Unsupported URI " + uri );
          }
          getContext().getContentResolver().notifyChange(uri, null);
          return rowsUpdated;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        int count = 0;

         switch (uriMatcher.match(uri)){
          case FTAGS:

              count = database.delete(TABLE_FTAGS, selection, selectionArgs);
              break;
          case FTAGS_ID:
              String id = uri.getLastPathSegment(); //gets the id
              count = database.delete( TABLE_FTAGS, COL_ID +  " = " + id + 
                    (!TextUtils.isEmpty(selection) ? " AND (" + 
                    selection + ')' : ""), selectionArgs);
              break;
          default: 
              throw new IllegalArgumentException("Unsupported URI " + uri);
          }

          getContext().getContentResolver().notifyChange(uri, null);
          return count;


    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }


}

该方法存在问题,还是ContentProvider出现问题?请帮忙

1 个答案:

答案 0 :(得分:1)

getContentResolver().update(FTagsContentProvider.CONTENT_URI, values, FTagsContentProvider.COL_TEXT + "=?", new String[]{mFtag});

是正确的用法。

public static final String COL_TEXT = "text";

text是保留关键字。您不能拥有名为text的列。更改列的名称,你会没事的。