繁体   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