0

我正在尝试将用户从屏幕上的 UITextFields 输入的值插入到我的 SQLite 数据库中。我能够打开数据库,创建表没有任何问题,但是当我尝试将值插入我的 SQLite 数据库时出现问题,我收到以下错误:

2013-06-25 14:35:12.074 BPApp[11317:c07] *** Assertion failure in -[BPAppMainViewController saveEntry:], /Users/capris/Documents/Documents/BPApp/BPApp/BPAppMainViewController.m:81
2013-06-25 14:35:12.075 BPApp[11317:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not update table'

这是我进行插入的方法:

- (IBAction)saveEntry:(id)sender {

int systolic = [_systolicText.text intValue];
int diastolic = [_diastolicText.text intValue];
NSString *comments = [_commentsText text];
NSDate *theDate = [NSDate date];

NSLog(@"%@, %d, %d, %@", theDate, systolic, diastolic, comments);

NSString *sql = [NSString stringWithFormat:@"INSERT INTO summary ('theDate', 'systolic', 'diastolic', 'comments') VALUES ('%@', '%d', '%d', '%@')", theDate, systolic, diastolic, comments];

char *err;

if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
    NSLog(@"Error message is: %s", sqlite3_errmsg(db));
    sqlite3_close(db);
    NSAssert(0, @"Could not update table");

} else {

    NSLog(@"table updated");
}

_systolicText.text = @"";
_diastolicText.text = @"";
_commentsText.text = @"";
}

谁能看到我做错了什么?我正在关注此处找到的一组教程。

我怀疑我的 SQL 语句可能有错误,但我似乎找不到任何问题。其他相关方法在这里:

- (void)viewDidLoad
{
    [self openDB];
    [self createTable:@"summary" withField1:@"theDate" withField2:@"systolic" withField3:@"diastolic" withField4:@"comments"];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}


- (void) createTable: (NSString *)tableName
          withField1: (NSString *)field1
          withField2: (NSString *)field2
          withField3:(NSString *)field3
          withField4:(NSString *)field4 {


    char *err;
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' " "TEXT PRIMARY KEY, '%@' INTEGER, '%@' INTEGER '%@' TEXT);", tableName, field1, field2, field3, field4];

    if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {

        sqlite3_close(db);
        NSAssert(0, @"Could not create table");

    } else {

        NSLog(@"Table created");

    }



}

-(void)openDB {

    if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK) {

        sqlite3_close(db);
        NSAssert(0, @"Database failed to open");

    }

    else {

        NSLog(@"database opened");
    }
}

- (NSString *)filePath {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"bp.sql"];

}

我的 sqlite3_errmsg 输出是:

2013-06-25 15:13:57.174 BPApp[12400:c07] Error message is: table summary has no column named comments

这对我来说没有意义。我似乎创造它就好了。

4

0 回答 0