sqlite prepare statement error - no such table

后端 未结 3 654
天命终不由人
天命终不由人 2021-01-14 19:45

I\'m having some difficulty with my sqlite prepare statement. I get an error saying my table does not exist, although I\'ve checked in multiple places for it, and it does ex

相关标签:
3条回答
  • 2021-01-14 20:14

    In terms of your immediate problem, it's going to be something simple.

    1. You say you've "cleaned and built again", but have you actually removed the old app from your simulator? Either remove the app manually, or, easier, just reset the simulator entirely by choosing "reset content and settings" from the "iOS Simulator" menu. Sometimes Xcode is not good about knowing what files to copy over (particularly in a case like this where your running it on the device may be changing the timestamp of the file in the simulator's bundle!)

    2. Run the app again.

    3. If the app doesn't work as expected, open up the database in the simulator folder from the Mac and check out the database to make sure the table is there and precisely as you expected it to be. So navigate to the app, open the bundle (you may have to choose the "show package contents" option), confirm the existence of the database, but just as importantly, open it up this particular copy of the database in your favorite Mac sqlite3 tool of choice and confirm the existence of the table there.

    Let us know what you find. Again, it's got to be something simple such as:

    • Perhaps the process of rebuilding the app was not reinstalling everything; I've occasionally had problems where Xcode elected to not re-copy something during the install on my simulator;

    • Perhaps your database in your project was accidentally put in a subdirectory, worse, you might have two copies sitting in different directories;

    • Perhaps the database in your Xcode project is missing (or has a typo or (esp in the case of the device) has incorrect filename capitalization) in the name of the table or file;

    • Etc.

    For a lot of these errors, you won't notice the problem until you completely reset the simulator itself. There are a million little things it could be, but hopefully completely resetting the simulator and starting over will help you find the issue. It's always something simple when it comes to these sorts of issues.


    Some other minor observations:

    1. You probably should not be opening databases from the bundle. Programmatically copy it from the bundle to the Documents folder, and open the database from there. I know it seems unnecessary, but it's important for a myriad of reasons (if db changes during operation of the app, if db accidentally gets created on you, don't let Xcode get confused about things that changed (even if only file timestamps) in the bundle changing behind Xcode's back, etc.)

    2. You should, if you need the database to be there, use sqlite3_open_v2, using either SQLITE_OPEN_READWRITE or SQLITE_OPEN_READONLY for flags (but do not include SQLITE_OPEN_CREATE). It causes headaches to ever give sqlite a chance to create a blank database for you, or otherwise modify it, so never give it an opportunity to so.

    0 讨论(0)
  • 2021-01-14 20:14

    I have encounter the same problem as yours. If the IOS can not find the designated database file, defaultly it will create one for you instead of throwing an error. So you must open the database file IOS created for you which is blank so it off course contain the table you expected. what I deal with it : 1 you have to bundle the resource file named *.sqlite3 into your project 2 Then You have to use [NSBundle mainBundle] pathFordirectory...... function to search your proper database file. then you can open the database file you expected and can operate it properly

    Best regards,

    0 讨论(0)
  • 2021-01-14 20:29

    Not enough rep to comment on Jack's post, but that helped me.

    In my case, I had mistyped my path for resource extension:

    // Wrong
    NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"productList"
                                                             ofType:@"sqlite3"];
    
    // Should have been (added db ext)
    NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"productList"
                                                         ofType:@"db"];
    

    I would always get past the:

    if (sqlite3_open([sqLiteDb UTF8String], &_database) == SQLITE_OK))
    

    because it was automatically creating a db file for me.

    0 讨论(0)
提交回复
热议问题