从Xcode中向SQLite3 DB添加数据

时间:2012-01-27 18:59:51

标签: objective-c xcode sqlite sqlitemanager

我正在尝试将3个数据添加到SQLite3数据库中。在教程的帮助下,我几乎就在那里。以下是我的代码。

我收到了NSLogs的期望。我发现数据库没有问题,我编译语句发送到数据库没有问题,但当应用程序到达sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK时,我希望数据被添加到DB,但它不是' t,我也期望显示UIAlertView告诉用户ERROR os SUCCESS。我希望你能看到我不知道的事情。

- (IBAction)addData:(id)sender {
    NSString *n = [[NSString alloc] initWithString:[name text]];
    NSString *a = [[NSString alloc] initWithString:[age text]];
    NSString *c = [[NSString alloc] initWithString:[color text]];
    NSLog(@"%@ - %@ - %@",n,a,c);
    [self insertDataName:n Age:a Color:c];
}

NSLog按预期返回文本属性。

- (IBAction)hide:(id)sender {
    [name resignFirstResponder];
    [age resignFirstResponder];
    [color resignFirstResponder];

}



- (void)viewDidLoad
{
    [super viewDidLoad];
    DBName = @"mydatabase.sqlite";
    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentsPaths objectAtIndex:0];
    DBPath = [documentsDir stringByAppendingPathComponent:DBName];

}


-(void)checkAndCreateDB {
    BOOL Success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    Success = [fileManager fileExistsAtPath:DBPath];

    if(Success) {
        NSLog(@"DB Found");    
        return;
    }

    NSLog(@"DB Not Found");
    NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];

}

NSLog会返回DB Found

-(void)insertData Name:(NSString*)n Age:(NSString*)a Color:(NSString*)c {
    [self checkAndCreateDB];
    sqlite3 *database;
    if (sqlite3_open([DBPath UTF8String],&database)==SQLITE_OK) {
        NSString *statement;
        sqlite3_stmt *compliedstatement;
        statement = [[NSString alloc] initWithFormat:@"insert into table1 values ('%@', '%@', '%@')",n,a,c];
        const char *sqlstatement = [statement UTF8String];
        NSLog(@"%@",statement);

        if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) {

        if (SQLITE_DONE!=sqlite3_step(compliedstatement)) {
            NSAssert1(0, @"Error by inserting '%s'", sqlite3_errmsg(database));
            UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error by inserting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];     

            [AlertOK show];

        }
        else {
            UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Data successfully inserted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];             
            [AlertOK show];
            }
        }
        sqlite3_finalize(compliedstatement);

    }
    sqlite3_close(database);

}

上面的NSLog上面显示insert into table1 values ('n', 'a', 'c')正如我所料。

插入时出现问题,我无法理解。

仅供参考使用SQLiteManager创建数据库。 SS下面:

SQLiteManager table1 SS

1 个答案:

答案 0 :(得分:1)

好的,您是在使用模拟器还是iDevice?在iDevice中,您无法创建外部SQL数据库(至少据我所知)。由于沙盒,您必须动态创建它。 “仅使用SQLiteManager创建数据库。”检查一下。

如果不是问题,请尝试将代码从“prepare ...”更改为“execute”

...从

if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) {

    if (SQLITE_DONE!=sqlite3_step(compliedstatement)) {
        NSAssert1(0, @"Error by inserting '%s'", sqlite3_errmsg(database));
        UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error by inserting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];     

        [AlertOK show];

    }
    else {
        UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Data successfully inserted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];             
        [AlertOK show];
        }
    }
    sqlite3_finalize(compliedstatement);

要...

    sqlite3_exec(database, [sqlstatement UTF8String], NULL, NULL, &error);

登录“错误”,因为这非常重要。

如果您想弹出UIAlert,请执行此操作

 if (error !=nil)
    //show alert for error
 else
    //show alert for success