Monica_G (28) [Avatar] Offline
#1
Hello Craig,

I am in the SQLite section of Chapter 11 and I am getting a compiler error stating:

Initializer for conditional binding must have Optional type, not 'FMDatabase'


on the line
guard let db = FMDatabase(path: booksFile.path) else {


in the getOpenDB() function.

I am wondering if something has changed in the latest version of fmdb which causes the code to no longer compile. Pulling the Chapter11.6.StoreDataSQL branch it compiles fine, but it has older versions of the fmdb files.

The FMDB readme on github has the following example for creating a db connection in Swift:

let database = FMDatabase(url: fileURL)

guard database.open() else {
    print("Unable to open database")
    return
}


I've modified my project to use that code (but with the path rather the url argument) and it compiles.


Monica


Craig Grummitt (49) [Avatar] Offline
#2
Hi Monica,

Thanks again for your feedback. You're great at finding these.

It seems as though a little while back when FMDB updated to the latest version (you can read their summary of changes in version 2.7 here) they decided to make the FMDatabase(path: ) method return a non-optional.

So, as you've noted, in this code:

guard let db = FMDatabase(path: booksFile.path) else {
            print("unable to create database")
            return nil
        }


It's now unnecessary to unwrap the database, so this can simply be replaced with:

let db = FMDatabase(path: booksFile.path)


In investigating this, I've discovered another change to the FMDB API by the way. The lastInsertRowId() method has been converted to a property. (You'll see this on page 321 of the book)

So the code which reads:
book.id = Int(db.lastInsertRowId())


Can be replaced with:
book.id = Int(db.lastInsertRowId)


I've updated this in the errata as well (at least I've requested it be updated, should be updated soon)

Thanks again!
Monica_G (28) [Avatar] Offline
#3
Awesome! Thank you!