0

我正在使用下面的代码片段将 html 字符串插入 sqlite db,我的代码工作正常,但是当我检索相同的 html 字符串并在 web 视图中显示时它不会呈现,一些数据正在被修改,任何人都可以帮助如何插入长 html 字符串到数据库中。

const char *sql = "插入文章 (art_id,sub_cat_id,art_title,art_details) 值(?, ?, ?, ?)";

                    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
                        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));

                    sqlite3_bind_text(addStmt, 1, [[tuser objectForKey:@"art_id"] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(addStmt, 2, [[tuser objectForKey:@"sub_cat_id"] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(addStmt, 3, [[tuser objectForKey:@"art_title"] UTF8String], -1, SQLITE_TRANSIENT);
                    sqlite3_bind_text(addStmt, 4, [[tuser objectForKey:@"art_details"] UTF8String], -1, SQLITE_TRANSIENT);


                    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
                        //coffeeID = sqlite3_last_insert_rowid(database);

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

1 回答 1

3

单引号可能会导致问题。由于它是 HTML,因此您可以将单引号替换为 & apos; 或者您可以像这里显示的那样“加倍”单引号,以便 sqlite 可以使用它:

    NSString *artdetails = [tuser objectForKey:@"art_details"];
    NSString *arttitle = [tuser objectForKey:@"art_title"];

    NSString *ad = [artdetails stringByReplacingOccurrencesOfString:@"'" withString:@"''"];    
    NSString *at = [arttitle stringByReplacingOccurrencesOfString:@"'" withString:@"''"];

...

    sqlite3_bind_text(addStmt, 3, [at UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(addStmt, 4, [ad UTF8String], -1, SQLITE_TRANSIENT);

ETC

于 2012-08-23T17:36:14.490 回答