2017-07-03 12 views
0

私はios devを初めて使用しており、次の問題が発生しています: 私は必要なテーブルを初期化しました。 "いいえこのようなテーブル"のエラーは、ここにステップとコードです:このテーブルはありません:初期化後でもsqliteのtablename

 -(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; 
} 

おかげで、ありがとう、ありがとうございます。この@try

+0

を呼び出すと、テーブル

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

作成し初期化した後、空白のファイル.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; } 

を追加すると、あなたのSQLite Fでありますileはドキュメントディレクトリにありますか? –

+0

はい私は何度もチェックしましたが、私は削除して、それがちょうどの場合に再作成されているかどうかをチェックしました。 私はテーブルを落として、シミュレータをクリアしましたが、何も動作していません。 – user3411226

答えて

0

は、初期化デシベルのために、この2つの機能を試してみてください。

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

この機能に問題があります。 – KKRocks

+0

いいえ、私はちょうど今試したことがあり、それは動作します! 本当にありがとうございますが、私はまだ新しいので、問題が何であるか把握するのに時間が必要な場合があります。 ありがとうございます! – user3411226

+0

これは間違った場所にデータベースを初期化しているためです。 si – KKRocks

0

Bool isSuccess = YES; 
if (sqlite3_open(path, &db) == SQLITE_OK) 
    { 

     char *errMsg; 
     const char *sql_stmt = 
     "CREATE TABLE IF NOT EXISTS contactTable (albumid integer primary key autoincrement ,directory CHAR(20) NOT NULL, albumname CHAR(32) NOT NULL, albumname CHAR(32) NOT NULL, count INT NOT NULL, orderid INT NOT NULL);"; 


     if (sqlite3_exec(db, sql_stmt, NULL, NULL, &errMsg) 
      != SQLITE_OK) 
     {   

      sqlite3_close(db); 
      isSuccess = NO; 

     } 

     sqlite3_close(db); 
     return isSuccess; 
    } 
    else {    

     sqlite3_close(db); 
     isSuccess = NO; 


    } 
} 


sqlite3_close(db); 
NSLog(@"Albums Table Created"); 
return isSuccess; 
関連する問題