插入sqlite语句成功但没有数据添加到数据库

时间:2015-02-02 11:02:30

标签: objective-c database sqlite

我有一个使用sqlite数据库的应用程序。我成功地从数据库中检索了所有数据但是当我尝试将数据插入数据库时​​,代码是正确的,并且没有调试问题。但是,当我检查数据库时,没有添加新行。即使我使用(sqlite3_last_insert_rowid)方法,我总是得到0! 我在这里尝试过许多与我类似的问题但没有运气的解决方案。大多数解决方案都提到数据库目录路径应该是可写的,我使用了他们提供的代码片段,但问题仍然存在。

这是我插入数据库的代码:

-(void)openConnection
{
    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    documentsDir = [paths objectAtIndex:0];
    dbPath = [documentsDir stringByAppendingPathComponent:@"databaseFile.sqlite"];
    NSLog(@"%@",dbPath);
    fileManager = [NSFileManager defaultManager];


BOOL success = [fileManager fileExistsAtPath:dbPath];

if (success)
    NSLog(@"we have the database");
else
{

    NSLog(@"we have no database");
    defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TLCWorkshopsApp.sqlite"];
    BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];
    if (moved)
        NSLog(@"database copied");

}

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
    NSLog(@"OPENED DB");
    void *v = NULL;
    char *errmsg;
    const char *sq = "PRAGMA foreign_keys = NO";
    if (sqlite3_exec(database, sq , 0, v, &errmsg)!= SQLITE_OK) {
        NSLog(@"faild to set the foreign_keys pargam");
    }

}
else
    NSLog(@"error in database");

}

-(void)addWorksopWithTitle:(NSString *)title presenters:(NSMutableArray *)presenters date:(NSString *)date time:(NSString *)time bld:(NSString *)bld floor:(int)floor room:(NSString *)room manadatory:(BOOL)manadatory
{
    [self openConnection];
    const char *encodeSqlStatement = "insert into Workshop (wid,title,manadatory,date,time,bld,floor,room) values(null,?,?,?,?,?,?,?)";
    //end of edited code
    sqlite3_stmt *compileStatement1;
    int success;
    if (sqlite3_prepare_v2(database, encodeSqlStatement, -1, &compileStatement1, NULL) == SQLITE_OK)
    {
        NSLog(@"insert WS prepair");
        sqlite3_bind_text(compileStatement1, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(compileStatement1, 2, manadatory);
        sqlite3_bind_text(compileStatement1, 3, [date UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(compileStatement1, 4, [time UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(compileStatement1, 5, [bld UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(compileStatement1, 6, floor);
        sqlite3_bind_text(compileStatement1, 7, [room UTF8String], -1, SQLITE_TRANSIENT);


    success = sqlite3_step(compileStatement1);


}else
{
    NSLog(@"insert WS NOT success");
    NSAssert1(0, @"Error: failed to prepare statement with message '%s'. ", sqlite3_errmsg(database));

}


sqlite3_finalize(compileStatement1);

if (success == SQLITE_ERROR)
{
    NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}


[self closeConnection];

}

1 个答案:

答案 0 :(得分:1)

经过几天的工作和搜索,我终于找到了我的问题的答案,并想与你分享。 我没有注意到sqlite3_step声明中收到的消息。我收到了(19)这意味着SQLITE_CONSTRAINT这意味着我试图将数据插入到表中的约束问题。 问题是我表中的主键是两个外键的组合。出于某种原因,我需要一个独立的主键。所以,我再次创建了表并添加了一个主键列。 这就是全部:))