Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite3_open always returning SQLITE_OK?

Curious about this... it seems that even if I change the pathForResource to @"fadfdasfa" or other non-existent name, I still am logging "Database Opened"?

sqlite3 * myDatabase;

NSString *path = [[NSBundle mainBundle] pathForResource:@"carsdatabase" ofType:@"db"];

    if (sqlite3_open([path UTF8String], &myDatabase) == SQLITE_OK)
        NSLog(@"Database Opened");
    else 
        NSLog(@"Failed to Open");
like image 515
sayguh Avatar asked Oct 31 '11 15:10

sayguh


2 Answers

The database is created for you if it does not already exist.

like image 79
勿绮语 Avatar answered Sep 18 '22 04:09

勿绮语


Open the database like this:

std::string filename("mydatabase.db");
sqlite3 *db;
int rc = sqlite3_open_v2(filename.c_str(), &db, SQLITE_OPEN_READWRITE, NULL);

Then it will return an error code (14) if the database file does not exist. However, if the file exists but is not a valid database, it returns SQLITE_OK!

like image 30
Rasoul Avatar answered Sep 17 '22 04:09

Rasoul