简体   繁体   English

使用目标c将html字符串插入sqlite db

[英]inserting html string into sqlite db using objective c

I am using the below snippet to insert html string into sqlite db, my code works fine but when i retrieve the same html string and display in web view it doesn't render, some data is getting modified, can anyone please help how to insert long html strings into db. 我正在使用以下片段将html字符串插入sqlite db,我的代码工作正常,但是当我检索相同的html字符串并在Web视图中显示时,它无法呈现,某些数据正在被修改,任何人都可以帮忙如何插入长html字符串到db中。

const char *sql = "insert into articles (art_id,sub_cat_id,art_title,art_details) Values(?, ?, ?, ?)"; const char * sql =“插入文章(art_id,sub_cat_id,art_title,art_details)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_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); 

The single quotes are likely causing the issue. 单引号可能会引起问题。 Since it's HTML you can replace the single quotes with an & apos; 由于是HTML,因此您可以将单引号替换为‘ or you can "double" the single quote like shown here so that sqlite is okay with it: 或者您也可以将单引号“加倍”,如下所示,以便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 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM