Monica_G (26) [Avatar] Offline
#1
Hi,

Sorry I am having more troubles with the SQLite section. I checkout out the Chapter11.6.SToreDataSQL branch https://github.com/iOSAppDevelopmentwithSwiftinAction/Bookcase/tree/Chapter11.6.StoreDataSQL. When I run the app I get the following error:

failed: no such table: Books


The path given to the file is:
/Users/monica/Library/Developer/CoreSimulator/Devices/5B886E05-1300-43FE-9F76-82CD5102BDF0/data/Containers/Data/Application/29E60DC9-8B98-40F8-AB0A-EE85A7A12C75/Library/Application Support/Books.db


If I open that database in the db browser, I see that it is empty. There is no books table.

However, if I open the "Books.db" file that is checked into the repository, I do see the books table, with one entry.

I tried "Erase all content and settings" on the simulator. I also checked in the "Build Phases" section that the Books.db was included in the "Copy bundled resources" section.

Any ideas what else to try?

Thank you,

Monica
Monica_G (26) [Avatar] Offline
#2
Not sure if this helps, but here is the output from doing a
ls -la
on the path:

ls -la /Users/monica/Library/Developer/CoreSimulator/Devices/5B886E05-1300-43FE-9F76-82CD5102BDF0/data/Containers/Data/Application/29E60DC9-8B98-40F8-AB0A-EE85A7A12C75/Library/Application\ Support/Books.db
-rw-r--r--@ 1 monica access_bpf 0 15 May 21:19 /Users/monica/Library/Developer/CoreSimulator/Devices/5B886E05-1300-43FE-9F76-82CD5102BDF0/data/Containers/Data/Application/29E60DC9-8B98-40F8-AB0A-EE85A7A12C75/Library/Application Support/Books.db
Monica_G (26) [Avatar] Offline
#3
Hmm, this is weird. I set a break point at the line:
if !FileManager().fileExists(atPath: filePath.path) {


and then stepped through code. I inspected the values of 'bundleFilePath' and 'filePath' at the line:
try FileManager().copyItem(at: bundleFilePath, to: filePath)


I opened the bundleFilePath file and it had the books table. I stepped past the line with no error and looked at the filePath Books.db file and it had the books table in it. I let the program continue running and it works! No errors. And if I rerun it works.

I'm not sure what happened and why it works now.
Monica_G (26) [Avatar] Offline
#4
Oh, wait I know what happened. I deleted the file before I did the break points.

Here's what happened:

- My version wasn't working (no books table as well)
- I pulled the version from github
- It had the same problem too (no books table). But the code checks to see if the Books.db file exists. Since it did (my broken one), it did not copy the Books.db file over.
- I deleted the file, so when I reran it, the Books.db file was copied over and now it works.
- When I go back into my version of the code it works too, as it's now using the file that was copied over in the step above.

So, I must have created a bad Books.db file at some point, and both versions of the app were using that (since the file existed). But now it works.

Sorry for the rambling thread.
Monica_G (26) [Avatar] Offline
#5
A bit more debugging tonight lead me to why I had the bad books.db file.

The code in the book for setting the booksFile property uses the following:

Bundle().resourceURL?.appendingPathComponent("Books").appendingPathExtension("db")


which returns a nil value.

The code in the repository uses:
 Bundle.main.resourceURL?.appendingPathComponent("Books").appendingPathExtension("db") { 


which works.

I'll add this to the errata thread.