1

我正在尝试将 3 条数据添加到 SQLite3 DB。在教程的帮助下,我快到了。下面是我的代码。

我收到了我对NSLogs. 我发现数据库没有问题,我编译语句以毫无问题地发送到数据库,但是当应用程序到达时,sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK我期望数据被添加到DB,但事实并非如此,我也期望显示aUIAlertView告诉用户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 表 1 SS

4

1 回答 1

1

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

如果这不是问题,请尝试将代码从“准备...”更改为“执行”

从...

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
于 2012-01-30T13:58:33.273 回答