sayguh
sayguh

Reputation: 2550

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");

Upvotes: 5

Views: 5889

Answers (3)

Rasoul
Rasoul

Reputation: 3847

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!

Upvotes: 7

勿绮语
勿绮语

Reputation: 9320

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

Upvotes: 8

M.Othman
M.Othman

Reputation: 5300

This is like when you run the following in the terminal:

sqlite3 test.db

If it's not there, it would be created for you. So you just create a new database every time you change the name and probably you will see it in your main bundle.

Upvotes: 1

Related Questions