简体   繁体   中英

Error creating SQLite DB in Objective-C

I am trying to build a SQLite DB in Xcode using Objective-c but am having an issue creating the table. I believe my insert function is done correctly, but when I try to run it I get the following error:

2016-09-20 09:39:31.612 Test[58546:5169036] Unable to prepare statement: no such table: Users

My code is here below. Please let me know if know what I may be doing incorrectly. Thanks!

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

}

Please carefully check your create table SQL, there are some issues I have found with your SQL:

  1. CREATE TABLLE (should be TABLE)
  2. Missing the closing parentheses at the end of the SQL.
  3. IF NOT EXIST (should be EXISTS)
  4. null should come at the end of field declaration. eg, PHONE TEXT NULL

Why not try and use FMDB , which is one of the most popular library to deal with SQLite in OC. And you don't need to use the low-level C code to handle SQLite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM