2016-09-20 1 views
0

Objective-cを使用してXcodeにSQLite DBを構築しようとしていますが、テーブルの作成に問題があります。私は挿入機能が正しく実行されたと信じていますが、実行しようとすると次のエラーが表示されます。Objective-CでSQLite DBを作成中にエラーが発生しました

2016-09-20 09:39:31.612テスト[58546:5169036] :ユーザー

私のコードは以下の通りです。私が間違って何をしているのか知っていれば教えてください。ありがとう!

[super viewDidLoad]; 
//do any addtional setup after view load, typically from nib 
NSString *docsDir; 
NSArray *dirPaths; 

//gets the directory 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 

//Build path to keep database 
_databasePath = [[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"Users.db"]]; 
NSFileManager *filemgr = [NSFileManager defaultManager]; 

    if([filemgr fileExistsAtPath:_databasePath] == NO) { 
    const char *dbPath = [_databasePath UTF8String]; 

    if(sqlite3_open(dbPath, &_DB) == SQLITE_OK) { 
     char *errorMessage; 
     const char *sqlStatement = "CREATE TABLLE IF NOT EXIST Users (Username TEXT PRIMARY KEY, PASSWORD TEXT, EMAIL TEXT, null PHONE TEXT, null WINS INTEGER, null LOSSES INTEGER, null BALANCE DECIMAL INTEGER"; 

     if(sqlite3_exec(_DB, sqlStatement, NULL, NULL, &errorMessage) != SQLITE_OK) { 
      //below creates a popup success message if necessary 
      UIAlertController * alert= [UIAlertController 
              alertControllerWithTitle:@"Success" 
              message:@"Table created" 
              preferredStyle:UIAlertControllerStyleAlert]; 

      UIAlertAction* ok = [UIAlertAction 
           actionWithTitle:@"OK" 
           style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * action) 
           { 
            [alert dismissViewControllerAnimated:YES completion:nil]; 

           }]; 

      [alert addAction:ok]; 
      [self presentViewController:alert animated:YES completion:nil]; 
     } 
     sqlite3_close(_DB); 
    } 
    else { 
     //below creates a popup error message if necessary 
     UIAlertController * alert= [UIAlertController 
             alertControllerWithTitle:@"Error" 
             message:@"Unable to create table" 
             preferredStyle:UIAlertControllerStyleAlert]; 

     UIAlertAction* ok = [UIAlertAction 
          actionWithTitle:@"OK" 
          style:UIAlertActionStyleDefault 
          handler:^(UIAlertAction * action) 
          { 
           [alert dismissViewControllerAnimated:YES completion:nil]; 

          }]; 

     [alert addAction:ok]; 
     [self presentViewController:alert animated:YES completion:nil]; 
    } 
} 

}

+0

ご迷惑をおかけして申し訳ありませんが、これを明確にするために私はDBを作成するためのコードです。私はこれが問題のどこにあるのかと考えています。私の挿入を実行しようとすると、 "Users"という表は作成されません。 – dgelinas21

+0

あなたはこれをどこで実行していますか? on Simulator?1。クリーンなビルドを行い、アプリをアンインストールしてもう一度実行してみてください。 2.問題が解決しない場合は、更新する前に選択したクエリを実行して、テーブル名が存在するかどうかを確認します。 –

答えて

0

慎重に作成するテーブルのSQLを確認してください、私はあなたのSQLを発見したいくつかの問題があります。

  1. は閉鎖を欠落TABLLE(TABLEでなければなりません)
  2. をCREATE SQLの最後にかっこが付いています。
  3. IF NOT EXIST(存在する必要があります)
  4. nullはフィールド宣言の末尾に来る必要があります。例:PHONE TEXT NULL

FMDBは、OCのSQLiteを処理するための最も一般的なライブラリの1つです。また、SQLiteを処理するために低レベルのCコードを使用する必要はありません。

+0

ありがとうございました!私はIOSアプリケーションにかなり新しいです、私はFMDBを見ていきます。 – dgelinas21

関連する問題