无法在iOS中更新SQLite数据库

时间:2013-06-25 18:56:34

标签: ios sql sqlite

我正在尝试将值插入到用户从屏幕上的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 = @"";
}

谁能看到我做错了什么?我正在按照here找到的教程集。

我怀疑我的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

这对我没有意义。我似乎正在创造它。

0 个答案:

没有答案