没有这样的表:sqlite中的tablename甚至在初始化之后

时间:2017-07-03 13:21:49

标签: ios objective-c sqlite

我是ios dev的新手,我遇到以下问题: 我初始化了我需要的表格,当我尝试对任何表格执行查询时,我得到了"没有这样的表格#34;错误,这是步骤和代码:

        -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Function called to create a copy of the database if needed.
                [[SQLiteUtilities sharedSQLiteManager] initializationDatabase];
                return YES;
            }

            and here is the implementation:

         -(void)initializationDatabase {
                NSString *path = [self databaseFilePath];
                BOOL isExists = [[NSFileManager defaultManager] fileExistsAtPath:path];

                if (isExists) return;

                FMDatabase *db = [FMDatabase databaseWithPath:path];
                if ([db open] == NO) {
                    return;
                } 
                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"
                ");";
                //[db executeQuery:sql1];
                [db executeQuery:sql];
                [db close];
            }

    and when i try to add anything as follows i get the "no such table:albums" error:

- (AlbumsUtilities *)createAlbumWithName:(NSString *)name {
    FMDatabase *db = [FMDatabase databaseWithPath:[self databaseFilePath]];
    if (![db open]) return nil;
    NSString *directory = createRandomAlbumDirectory();

    int maxid = 0;
    BOOL success = [db executeUpdate:@"INSERT INTO albums(directory,albumname,count,orderid) VALUES(?,?,0,?)", directory,name,@(maxid)];
    AlbumsUtilities *album = nil;
    if (success) {
       ....(not getting here of course)
    }
    [db close];
    return album;
}

任何帮助都将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:0)

@试试这个:

答案 1 :(得分:0)

尝试使用这两个函数初始化db。

添加空白文件.sqlite扩展程序

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

    success = [fileManager fileExistsAtPath:[self getDBPath]];
    if (success){
        return;
    }
    // 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 getDBPath] error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}


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

初始化创建表格后

-(void)createTable{
    FMDatabase *database = [FMDatabase databaseWithPath:[self getDBPath]];
    [database open];
    [database executeUpdate:"your query"];
    [database close];
}

<强>调用

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Function called to create a copy of the database if needed.
            [[SQLiteUtilities sharedSQLiteManager] createCopyOfDatabaseIfNeeded];
       [[SQLiteUtilities sharedSQLiteManager] createTable];
}