objective c - iOS app Sqlite Prepare statement error when inserting long html string -


i facing issue when inserting long html string in sqlite database in ios app.

i getting long html content dynamically pulled mail server. example html content below.

[i need store entire html string. because i'll have string again , load in uiwebview]

    <div style="padding-bottom: 20px;"><div style="background-color:#eee">                                       <div><b>from:</b> martin test &lt;martintest@gmail.com&gt;</div>                                                        <div><b>to:</b> steve test &lt;stevetest@yahoo.com&gt;, martin test &lt;martintest@gmail.com&gt;</div>                                                                                                       <div><b>subject:</b> testing</div>                                                    <div><b>date:</b> july 8, 2015 @ 11:05:05 gmt+5:30</div>                  </div></div><div><html xmlns="http://www.w3.org/1999/xhtml">             <head>             <title></title>             </head>             <body>             <div dir="ltr">this testing email.<br />             <div><br /></div>             <div>how you?</div>             <div><br /></div>             <div>best of luck.</div>             <div><br /></div>             <div>bye take care.</div>             <div><br /></div>             <div><br /></div>             <div>regards,</div>             <div>martin</div>             </div>             </body>             </html>             </div> 

when trying insert sqlite database below,

    - (void) storemailcontent:(nsstring*)emailaddress foldername:(nsstring*)foldername msguid:(unsigned int)msguid msgcontent:(nsstring *)msgcontent {  // msgcontent html long string          const char *dbpath = [databasepath utf8string];                  if (sqlite3_open(dbpath, &database) == sqlite_ok)                 {                     nsstring *insertsql = [nsstring stringwithformat:@"insert offlinemailsdbtable (emailid, foldername, uid, content) values (\"%@\", \"%@\", %d, \"%@\")", emailaddress, foldername, (int)msguid, msgcontent];                     nslog(@"insertsql: %@", insertsql);                      if (sqlite3_prepare(database, [insertsql utf8string], -1, &statement, null) ==sqlite_ok)                     {                         sqlite3_bind_text(statement, 1, [emailaddress utf8string], -1, sqlite_transient);                         sqlite3_bind_text(statement, 2, [foldername utf8string], -1, sqlite_transient);                         sqlite3_bind_int(statement, 3, (int)msguid);                         sqlite3_bind_text(statement, 4, [msgcontent utf8string], -1, sqlite_transient);                           if (sqlite3_step(statement)==sqlite_done)                         {                             nslog(@"inserted values in table");                         }                         else                         {                             nslog(@" not inserted values in table");                             nslog(@"error: %s", sqlite3_errmsg(database));                         }                     }                     else                     {                         nslog(@"problem prepare statement: %s", sqlite3_errmsg(database));                     }                     sqlite3_finalize(statement);                     sqlite3_close(database);                     nslog(@"db closed");                 }      } 

it giving error as,

problem prepare statement: near "padding": syntax error 

when executing below line,

nsstring *insertsql = [nsstring stringwithformat:@"insert offlinemailsdbtable (emailid, foldername, uid, content) values (\"%@\", \"%@\", %d, \"%@\")", emailaddress, foldername, (int)msguid, msgcontent]; 

you have escape double quotes in html content before adding insert statement.

assuming html content stored in variable msgcontent should like:

nsstring *escapedmsgcontent = [msgcontent stringbyreplacingoccurrencesofstring:@"\"" withstring:@"\\\""]; nsstring *insertsql = [nsstring stringwithformat:@"insert offlinemailsdbtable (emailid, foldername, uid, content) values (\"%@\", \"%@\", %d, \"%@\")", emailaddress, foldername, (int)msguid, escapedmsgcontent]; 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -