使用目标c将html字符串插入sqlite db [英] inserting html string into sqlite db using objective c

查看:138
本文介绍了使用目标c将html字符串插入sqlite db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码段将html字符串插入sqlite db,我的代码工作正常,但是当我检索相同的html字符串并在Web视图中显示时,它无法呈现,某些数据正在被修改,任何人都可以帮忙如何在数据库中插入较长的html字符串.

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.

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

const char *sql = "insert into articles (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);

推荐答案

单引号可能会引起问题.由于是HTML,因此您可以用&替换单引号. ‘或者,您也可以将单引号加倍",如下所示,以便sqlite可以:

The single quotes are likely causing the issue. Since it's HTML you can replace the single quotes with an & apos; or you can "double" the single quote like shown here so that sqlite is okay with it:

    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);

这篇关于使用目标c将html字符串插入sqlite db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆