【问题标题】:Error creating SQLite DB in Objective-C在 Objective-C 中创建 SQLite DB 时出错
【发布时间】:2017-01-28 11:31:35
【问题描述】:

我想建立一个SQLite数据库在Xcode使用Objective-C,但我有创建表的问题。我相信我的插入功能做得正确,但是当我尝试运行它,我得到以下错误: P>

二○一六年九月二十○日09:39:31.612试验[58546:5169036]无法准备语句:没有这样的表:用户 P>

我的代码是在这里的下方。请让我知道,如果知道我可能做不正确。谢谢!

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

}

【问题讨论】:

  • 对不起任何混乱,只是为了澄清,这是我的用于创建数据库的代码。我相信这是那里的问题,因为它说没有创建表“用户”尝试运行我插入的时候。 SPAN>
  • 你在哪里运行呢?在模拟器?1。尝试做一个干净的构建和卸载应用程序,并再次运行? 2.如果问题仍然存在,尝试更新之前运行选择查询,看看你的表名存在? SPAN>

标签: objective-c iphone xcode sqlite


【解决方案1】:

请仔细检查您的创建表 SQL,我发现您的 SQL 存在一些问题:

  1. 创建TABLLE(应该是表)
  2. 缺少 SQL 末尾的右括号。
  3. 如果不是 EXIST(应该是 EXISTS)
  4. null 应该出现在字段声明的末尾。例如,PHONE TEXT NULL

为什么不尝试使用FMDB,它是OC 中处理SQLite 的最流行的库之一。并且您不需要使用低级 C 代码来处理 SQLite。

【讨论】:

  • 感谢您的帮助!我对IOS应用比较陌生,我会看看FMDB。
猜你喜欢
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
  • 2012-12-21
  • 2019-04-19
  • 1970-01-01
相关资源
最近更新 更多