sqlite3_step(statement)每次都给出false

时间:2016-08-31 05:59:52

标签: ios objective-c xcode sqlite

我的课程中有这种方法将数据插入表格

- (BOOL) registerData:(NSString*)name
                email:(NSString *)email mobno:(NSString*)mobno username:(NSString *)username password:(NSString *)password imageUrl:(NSString*)imageUrl;
{
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat:@"insert into loginDetail (name, email, mobno, username, password, imageUrl) values (\"%@\",\"%@\", \"%@\", \"%@\", \"%@\", \"%@\")", name,email,mobno,username,password,imageUrl];
        NSLog(@"image path %@", imageUrl);
        const char *insert_stmt = [insertSQL UTF8String];
        if (sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL) == SQLITE_OK) {

        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"SQLITE DONE %d", sqlite3_step(statement));
            return YES;
        }
        else {
            NSLog(@"SQLITE DONE %d", sqlite3_step(statement));
            return NO;
        }
        sqlite3_reset(statement);
    }
    }
    sqlite3_close(database);
    return NO;
}

sqlite3_step(声明)每次都返回false。

我从

调用此方法
- (IBAction)registerBtn:(UIButton *)sender {
    NSLog(@"fileName path = %@", fileName);
    wasClicked = YES;
    BOOL success = NO;
    if(wasClicked)
    {
        if((_errorMessageLabel.hidden)) {
            NSLog(@"register button clicked");

            NSString *alertString = @"Data Insertion failed";
            if (name.text.length>0 && email.text.length>0 &&
                mobileNo.text.length>0 && Useraname.text.length>0  && password.text.length>0 && fileName.length>0)
            {
                success = [[DatabaseConnection getSharedInstance]registerData:
                           name.text email:email.text mobno:
                           mobileNo.text username:Useraname.text password:password.text imageUrl:fileName];
                NSLog(success? @"yes" : @"no");
                if(success == YES)
                {
                    _successLbl.text = @"Data Registered Successfully";

                    name.text = @"";
                    email.text =@"";
                    mobileNo.text = @"";
                    Useraname.text =@"";
                    password.text =@"";
                }
            }
            else{
                alertString = @"Enter all fields";
            }
            if (success == NO) {

                alertString = @"Data not registered, Duplicate Username Found!";
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:
                                      alertString message:nil
                                                              delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
        }
    }
}

但每次执行代码时,我都会获得成功。

不知道为什么但是在我在imageURL的数据库中添加新字段后会发生。

我的创建数据库查询是,

const char *sql_stmt = "create table if not exists loginDetail (name text, email text, mobno text, username text primary key, password text, imageUrl text)";

请建议更改。

1 个答案:

答案 0 :(得分:0)

我只需更改表格并在其中添加新列。

对于我使用的,

const char *sql_stmt = "create table if not exists loginDetail (name text, email text, mobno text, username text primary key, password text)";

            const char *newLoginDetail = "ALTER TABLE loginDetail RENAME TO newLoginDetail";
            const char *altr_stmt = "ALTER TABLE newLoginDetail ADD COLUMN imageUrl text";
            const char *sql_stmt1 = "create table if not exists newLoginDetail (name text, email text, mobno text, username text primary key, password text, imageUrl text)";

现在创建了新列,它可以正常工作。