插入到db无法在iOS中运行?

时间:2014-05-27 18:15:06

标签: ios objective-c database sqlite insert

我在iOS中使用SQLite数据库。我已经成功创建了数据库和表。但是,当我尝试将文本字段内容保存到数据库中但显示“无法添加联系人”警报时。

我对INSERT字符串以及我在其中分配文本字段值的方式有疑问。

任何人都可以帮我解决这个问题吗?

    - (IBAction)save:(id)sender {
    sqlite3_stmt    *statement;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK) {
        NSString *insertSQL = [NSString stringWithFormat:
                               @"INSERT INTO LubnaNewUser (name,address,phonenumber,tier) VALUES (\"%@%@%@%@\")",
                               name.text,address.text,phonenumber.text,tier.text];

        const char*insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(myDatabase, insert_stmt,  -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE) {
            statusOfAddingToDB = [NSString stringWithFormat:@"Text added -- %@", name.text];
        } else {
            statusOfAddingToDB = @"Failed to add contact";
        }

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"DB Status" message: statusOfAddingToDB delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];

        sqlite3_finalize(statement);
        sqlite3_close(myDatabase);
    }
}

enter image description here 新代码 enter image description here

 if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK)
    {
        //Select all from SAMPLETABLE. This includes the 'id' column and 'message' column.
        NSString *querySQL = @"SELECT * FROM LubnaNewUser";
        const char *query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(myDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {
                NSString *text = [[NSString alloc]
                                  initWithUTF8String:(const char *) sqlite3_column_text(statement, 3)]; //Num 1 means on what column. Column 0 = 'id' column while Column 1 = 'message' column in our query result.
                [list addObject:text];
                //NSLog(@"count: %d", [list count]);
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(myDatabase);
    }

1 个答案:

答案 0 :(得分:1)

致电sqlite3_errmsg以收到错误讯息。

无论如何,你必须在VALUES子句中写入四个值:

NSString *insertSQL = [NSString stringWithFormat:
     @"INSERT INTO LubnaNewUser (name,address,phonenumber,tier) VALUES ('%@','%@','%@','%@')",
     name.text,address.text,phonenumber.text,tier.text];

然而,当任何这些字符串包含引号时,这将会爆炸。 更好地使用参数:

const char *insertSQL = "INSERT INTO LubnaNewUser (name,address,phonenumber,tier) VALUES (?,?,?,?)";
sqlite3_prepare_v2(myDatabase, insertSQL, -1, &statement, NULL);
sqlite3_bind_text(statement, 1, [name.text        UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 1, [address.text     UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 1, [phonenumber.text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 1, [tier.text        UTF8String], -1, SQLITE_TRANSIENT);
...