Objective C中的SQLite保存字符串

时间:2011-06-14 10:15:10

标签: iphone objective-c sqlite nsstring

您好我正在尝试将字符串保存到SQLite数据库中,但没有任何反应。我有一个字符串:

  

“tret44404%40ukr.net%3A100001740940428%3A06Kz8p1Xbk0PzpH%2F%3A0%3Av_1%2Cajax_1%2Cwidth_320%2Cpxr_1%2Cgps_1%3A1307716802”

我正在尝试将其保存到数据库,但它将字符串保存为:

  

“tret44404
  80792kr.net-0X1.FD428P + 01000017409404280X1.B2C260000001P-100906Kz8p1Xbk0PzpH0.0000000X1.149242P-104700X1.400000018P-1040v_1灠ajax_1Ῐwidth_320"

我如何保存字符串的方法:

- (void) addCookie {


    if(addStmt == nil) {
        const char *sql = "insert into FaceBookCookies(userId, created, domain, Expires, HttpOnly, Name, Path, Value) Values(?, ?, ?, ?, ?, ?, ?, ?)";
        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
    }
    sqlite3_bind_int(addStmt, 1, userID);
    sqlite3_bind_int(addStmt, 2, created);
    sqlite3_bind_text(addStmt, 3, [domain UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_double(addStmt, 4, [Expires timeIntervalSince1970]);
    sqlite3_bind_text(addStmt, 5, [HttpOnly UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_text(addStmt, 6, [Name UTF8String], -1, SQLITE_STATIC);
    sqlite3_bind_text(addStmt, 7, [Path UTF8String], -1, SQLITE_STATIC);

    sqlite3_bind_text(addStmt, 8, [Value UTF8String], -1, SQLITE_STATIC); //here is a String...

    if(SQLITE_DONE != sqlite3_step(addStmt))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
        //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
        userID = sqlite3_last_insert_rowid(database);

    //Reset the add statement.
    sqlite3_reset(addStmt);
}

请帮忙解决问题......

1 个答案:

答案 0 :(得分:0)

您必须在保存之前删除百分比转义符。这样做,

sqlite3_bind_text(addStmt, 8, [[Value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] UTF8String], -1, SQLITE_STATIC);