0

i am working with xcode objective c and i have the following code:

- (void)initializationDatabase {
    NSString *sql =
    @"CREATE TABLE albums("
    "albumid INTEGER PRIMARY KEY AUTOINCREMENT,"
    "directory CHAR(20) NOT NULL,"
        "albumname CHAR(32) NOT NULL,"
        "count INT NOT NULL,"
        "orderid INT NOT NULL"
        ");"

        "CREATE TABLE photos("
        "photoid INTEGER PRIMARY KEY AUTOINCREMENT,"
        "albumid INTEGER NOT NULL DEFAULT 0,"
        "filename CHAR(50) NOT NULL DEFAULT \"\","
        "originalname CHAR(50) DEFAULT \"\","
        "addtime INTEGER NOT NULL DEFAULT 0,"
        "createtime INTEGER NOT NULL DEFAULT 0,"
        "filesize INTEGER NOT NULL DEFAULT 0,"
        "filetype TINYINT NOT NULL DEFAULT 0"
        ");";
        FMDatabase *database = [FMDatabase databaseWithPath:[self databaseFilePath]];
        [database open];
        [database executeUpdate:sql];
        [database close];
    }


    - (void)createCopyOfDatabaseIfNeeded {
        // First, test for existence.
        BOOL success;
        NSError *error;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSLog(@"%@",[self databaseFilePath]);

        success = [fileManager fileExistsAtPath:[self databaseFilePath]];
        if (success){
            return;
        }else{
            [fileManager createFileAtPath:[self databaseFilePath] contents:nil attributes:nil];
        }
        // The writable database does not exist, so copy the default to the appropriate location.
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"db.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:[self databaseFilePath] error:&error];
        if (!success) {
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }
    }

-(NSString *)databaseFilePath{
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"FakeCalculator.sqlite"];
    return  dbPath;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Function called to create a copy of the database if needed.
    [[SQLiteUtilities sharedSQLiteManager] createCopyOfDatabaseIfNeeded];
    [[SQLiteUtilities sharedSQLiteManager] initializationDatabase];

    return YES;
}

but my problem is that this code is creating only the table albums and not the table photos, when i try to insert delete or anything to table photos i get the table does not exist error, and when i open the db.sqlite file i only find one table in it (albums), any ideas?

1 Answer 1

1
[database executeUpdate:sql];

This executes a single SQL statement.

To execute multiple statements, use executeStatements:.

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

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.