Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
806 views
in Technique[技术] by (71.8m points)

sqlite - No Such Table error

I had to search for many posts regarding these errors, but still I cannot fix the problem. Here is my code, can anyone help me to see what is going wrong?

- (void) copyDatabaseIfNeeded {


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *path = [documentsDirectory stringByAppendingPathComponent:@"SQL.sqlite"];

    if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) 
    {
        NSLog(@"Database opened successfully");

if(updateStmt == nil) {
            NSString *updStmt = [NSString stringWithFormat: @"UPDATE Coffee SET CoffeeName = '500 Plus', Price = '1.40' Where CoffeeID= '3'"];
   const char *mupdate_stmt = [updStmt UTF8String]; 

if(sqlite3_prepare_v2(newDBconnection, mupdate_stmt, -1, &updateStmt, NULL) != SQLITE_OK){
NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(newDBconnection));
            } else {
                NSLog(@"Update successful");
            }

        }
        if(SQLITE_DONE != sqlite3_step(updateStmt))
            NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(newDBconnection));
        else {
            sqlite3_reset(updateStmt);
            NSLog(@"Step successful");

        }
    } 
    else 
    {
        NSLog(@"Error in opening database  ");
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no table Coffee in SQL.sqlite.

SQLite silently creates the database file if it does not exist. So if you've got the path wrong, you are opening an empty database file, which of course does not contain any tables. Make sure the database file exists there and it is not empty.

You can see what tables are there in the database by running SELECT * FROM sqlite_master; query.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...