Search code examples
iossqlitefmdb

Warning: I could not find the column named 'rowid'


I use FMDB in my iOS project. But when I read ROWID with FMDB,the Xcode log "Warning: I could not find the column named 'rowid'."

...
 //Create database
 NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *dbPath = [documentsPath   stringByAppendingPathComponent:@"bookmarksDatabase.sqlite"];

    BOOL needCreateTable = ![[NSFileManager defaultManager] fileExistsAtPath:dbPath];
    FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
    [db open];
    if (needCreateTable) {

        [db executeUpdate:@"CREATE TABLE bookmarks (title TEXT ,url TEXT ,folderID INTEGER ,locationIndex INTEGER)"];
    }
    else
    {
        [self reloadBookmarkDatabase:db];

    }

        [db close];

   ...
   //Read database. Only "ROWID" column can't find.
FMResultSet *results = [db executeQuery:@"SELECT * FROM bookmarks ORDER BY locationIndex"];
    while([results next]) {
        BookmarkData *temp  = [[BookmarkData alloc] initWithID:[results intForColumn:@"ROWID"] title:[results stringForColumn:@"title"] url:[results stringForColumn:@"url"] folderID:[results intForColumn:@"folderID"] locationIndex:[results intForColumn:@"locationIndex"]];
        [self.bookmarkArray addObject:temp];

        NSLog(@"bookmark id:%d, title:%@, url:%@, folderID:%d, locationIndex:%d",temp.ID,temp.title,temp.url,temp.folderID,temp.locationIndex);
    }

Solution

  • SELECT * returns only the columns from the table definition.

    If you want to get the rowid column, you have to list it explicitly:

    SELECT *, rowid FROM ...
    

    And if you actually need to use this column, it would be a better idea to make it part of the table definition:

    CREATE TABLE bookmarks (
        ID INTEGER PRIMARY KEY,    -- same as rowid
        title TEXT,
        ...
    )