在sqlite数据库中插入数据的问题

时间:2013-04-24 07:56:31

标签: ios sqlite

这就是我尝试在sqlite数据库中添加数据的方法一切正常但数据没有添加到数据库中我不明白你为什么可以参考代码并告诉我我做错了什么

    sqlite3_stmt *stmt;
    int x;

    char *update = "insert into PersonNamesAndBirthDates (Names,Birthdates,Phonenumber,Email,Profilepic) values(?,?,?,?,?);";
    x = sqlite3_prepare_v2(database1, update, -1, &stmt, nil);

    if (x == SQLITE_OK)
    {
        NSLog(@"PersonName is -->%@",[NSString stringWithFormat:@"%@",[_Namebarray objectAtIndex:0]]);
        NSLog(@"BirthDates is -->%@",[NSString stringWithFormat:@"%@",[_Birthdatebarray objectAtIndex:0]]);
        NSLog(@"BirthDates is -->%@",[NSString stringWithFormat:@"%@",[_Phonearray objectAtIndex:0]]);
        NSLog(@"BirthDates is -->%@",[NSString stringWithFormat:@"%@",[_Emailarray objectAtIndex:0]]);
        // if else will come here for checking weather images has added or not if added then store Yes Image if not then simply No image for default image.


        sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:@"%@",[_Namebarray objectAtIndex:0]] UTF8String],-1, NULL);
        sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:@"%@",[_Birthdatebarray objectAtIndex:0]] UTF8String],-1, NULL);
        sqlite3_bind_text(stmt, 3, [[NSString stringWithFormat:@"%@",[_Phonearray objectAtIndex:0]] UTF8String],-1, NULL);
        sqlite3_bind_text(stmt, 4, [[NSString stringWithFormat:@"%@",[_Emailarray objectAtIndex:0]] UTF8String],-1, NULL);
        if(selectImage.image)
        {
            sqlite3_bind_text(stmt, 5, [[NSString stringWithFormat:@"%@",[_arrayOfPaths objectAtIndex:0]] UTF8String],-1, NULL);
        }
        else{
            sqlite3_bind_text(stmt, 5, [[NSString stringWithFormat:@"%@",@"No Image"] UTF8String],-1, NULL);
        }
    }
    if (sqlite3_step(stmt) != SQLITE_DONE){}
    NSLog(@"Error: ");
    sqlite3_finalize(stmt);

2 个答案:

答案 0 :(得分:1)

我看到您的代码中没有错误,请检查您的数据库是否像这样打开

if (sqlite3_open(dbpath, &database1) == SQLITE_OK)

答案 1 :(得分:0)

这里有一个使用SQLite的示例项目:https://github.com/AaronBratcher/ABSQLite

它有代码用于添加和读取数据。