簡體   English   中英

在Objective-C中創建SQLite數據庫時出錯

[英]Error creating SQLite DB in Objective-C

我正在嘗試使用Objective-c在Xcode中構建SQLite數據庫,但是在創建表時遇到了問題。 我相信我的插入功能正確完成,但是當我嘗試運行它時,出現以下錯誤:

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

}

請仔細檢查您的創建表SQL,發現您的SQL存在一些問題:

  1. 創建TABLLE (應該是表)
  2. SQL末尾缺少右括號。
  3. 如果不EXIST (應該存在)
  4. null應該出現在字段聲明的末尾。 例如, PHONE TEXT NULL

為什么不嘗試使用FMDB呢?FMDB是OC中處理SQLite的最流行的庫之一。 而且您不需要使用低級C代碼來處理SQLite。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM