0

Here is my code to add a table in my existing database. My code reaches to success callback of sqlite3_prepare_v2 and gives the log table created, but when I open my sqlite database using terminal, I see that there were no tables created. My database table name is fz1.sqlite declared by pragma by this: #define kDBName @"fz1.sqlite"

-(sqlite3 *)mydb:(NSString *)query{



static sqlite3 *database;
static sqlite3_stmt *enableForeignKey;



if (database == NULL) {
    sqlite3 *newDBconnection;


    // first get the path for the document directory of the application
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *dbpath = [paths objectAtIndex:0];
    NSString *path = [dbpath stringByAppendingPathComponent:kDBName];


    if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
        NSLog(@"Database Successfully Opened :)");
        database = newDBconnection;

        if (sqlite3_prepare_v2(database,[query UTF8String], -1, &enableForeignKey, NULL) != SQLITE_OK) {
            NSLog(@"ERROR IN PRAGMA!");
        }
        else{

            sqlite3_exec(database, [query UTF8String], NULL, NULL, NULL);

            NSLog(@"faiz's table created");
        }

        sqlite3_finalize(enableForeignKey);

    } else {
        NSLog(@"Error in opening database :(");
        database = NULL;
    }
}

return database;
  }
2
  • 1
    sqlite3 will tell you what went wrong if you ask it. Start doing that and you will get closer to solving your issues. Commented Jul 7, 2014 at 8:39
  • Print out where you're opening the DB. For a SQLite newbie odds are better than 50:50 that you're not opening the file where you think it is. Commented Jul 7, 2014 at 11:17

2 Answers 2

1

It seems no error with your implementation. I think you are opening the db file placed in the Application bundle and it will bot be updated with the queries you performing.

Once you launch your application the database file will be copied to the device/simulator 's document directory. So if you are running your application in iOS Simulator check the db in the document directory folder.

Open it and check whether updated or not.

Example of the folder structure for Document Dir with db file iOS Simulator

/Users/XYZ/Library/Application Support/iPhone Simulator/7.1/Applications/B5EC1893-B38F-4AB4-AE7C-48EF685EE35F/Documents/db.sqlite

Sign up to request clarification or add additional context in comments.

1 Comment

The OP is opening the database in the documents directory, not app bundle.
1

If you're putting parameters into your query using [NSString stringWithFormat:@"YourSqlStatement %@ %@",parameter1,parameter2]; or something similar before calling -(sqlite3 *)mydb:(NSString *)query you might be SQL injecting yourself. If you are doing this then try using sqlite_bind_text to load your parameters and then sqlite_step to execute.

If this isn't relevant to your implementation try logging the output of sqlite_exec and have a look at your error message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.