iPhone SQLite ‘no such table’ error
I recently ran into an annoying problem getting data from an SQLite database in my iPhone application. I had bundled the database with the application and duplicated code from Apple’s example code. No matter what I did every time I tried to select from my table I would get “no such table: table_name”. I tried checking what tables existed in the database using “SELECT name FROM sqlite_master WHERE type = ‘table’”, and found there were none.
What happened is sqlite3_open() will create a new database if it can’t find the one you requested. It will also cache this empty database and use it in the simulator. So even though I had fixed the issue with my code, that empty database was still being used. The solution to this is to delete the app from the simulator (click and hold the app icon, then click the x) and re-build. This will clear the cached database and use the copy bundled with your app.
Hopefully this will help someone else avoid a few hours of annoyance. It seems to be a fairly common pitfall, but I wasn’t able to find much in the way of answers.
Tags: cocoa, iPhone, nosuchtable, programming, sqlite, xcode
I did everything clear, uninstall, reset simulator.. but still getting same error. It creates EMPTY DB before the sqlite3_open() statement..and I am surprised with that. Any Clue..??
Never mind…I found the problem..Thanks.
same problem,
did all the things,
deleted app from simulator, cleaned all the targets, deleted reference , made newer reference .. then also m facing the same thing,
in this case it got suddenly stopped .
i havent updated DB schema..
@abc If you delete the app from the simulator that should remove any databases. Then make sure that your database is bundled with your app and then you’re opening that database without any typos like I did.
You can always run “SELECT name FROM sqlite_master WHERE type = ‘table’” and check if there is anything in your database. If not either your bundled DB is empty, the OS is caching an empty version, or you’re not selecting the right DB name.