ios - Save image data to sqlite database in iphone -
i want save image data sqlite database can't ...
- (void) savedata: (article*) article :(nsmutablearray*) rsslist { sqlite3_stmt *statement; if (sqlite3_open([self.databasepath utf8string], &articlesdb) == sqlite_ok) { nsstring* sqlformat = @"insert articles (guid, title, summary, mainlink, pubdate, author, imagelink, body, favorites, smallimage) values ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', ?)"; // correct summary. nsstring* summary = [nsstring stringwithstring:article.summary]; summary = [summary stringbyreplacingoccurrencesofstring:@"'" withstring:@"''"]; // correct title nsstring* title = [nsstring stringwithstring:article.title]; title = [title stringbyreplacingoccurrencesofstring:@"'" withstring:@"''"]; // correct body nsstring* body = [nsstring stringwithstring:article.body]; //body = [body stringbyreplacingoccurrencesofstring:@"'" withstring:@"''"]; // create insert sql query. nsstring* insertsql = [nsstring stringwithformat:sqlformat, article.guid, title, summary, article.mainlink, article.pubdate, article.author, article.imagelink, body, article.favorite ? @"yes" : @"no"]; // add database. sqlite3_prepare_v2(articlesdb, [insertsql utf8string], -1, &statement, null); if (sqlite3_step(statement) == sqlite_done) { // add list. [rsslist addobject:article]; // write log. nslog( @"article added ... " ); } else { nslog( @"failed add article. error is: %s", sqlite3_errmsg(articlesdb) ); } sqlite3_finalize(statement); sqlite3_close(articlesdb); } }
you can see have query , @ end have smallimage , value (?)
@"insert articles (guid, title, summary, mainlink, pubdate, author, imagelink, body, favorites, smallimage) values ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', ?)";
i have function must update image value somewhere in program: in function select row image link url when try write data last column "smallimage" no result ... when open database file editor see in last row "null" data not saved ...
- (void) savesmallimage: (nsdata*) imgdata :(nsstring*) mainurl { if (sqlite3_open([self.databasepath utf8string], &articlesdb) == sqlite_ok) { nsstring* sqlitequery = [nsstring stringwithformat:@"select * articles imagelink = '%@'", mainurl]; sqlite3_stmt* statement; if( sqlite3_prepare_v2(articlesdb, [sqlitequery utf8string], -1, &statement, null) == sqlite_ok ) { sqlite3_bind_blob(statement, 9, [imgdata bytes], [imgdata length], sqlite_transient); sqlite3_step(statement); } else { nslog( @"savebody: failed sqlite3_prepare_v2. error is: %s", sqlite3_errmsg(articlesdb) ); } // finalize , close database. sqlite3_finalize(statement); sqlite3_close(articlesdb); } else { nslog(@"savesmallimage: failed sqlite3_open. error is: %s", sqlite3_errmsg(articlesdb) ); } }
who can tell me wrong ...
here how can save image sqlite:
// create images table. sql_stmp = "create table if not exists images (url text unique, image blob)"; if( sqlite3_exec(articlesdb, sql_stmp, null, null, &errmsg) != sqlite_ok ) nslog( @"fail create \"images\" table. error is: %@", errmsg );
and here function save:
// save small image data given main url - (void) saveimagestosql: (nsdata*) imgdata :(nsstring*) mainurl { nslog( @"\n*****save image sqlite*****\n" ); const char* sqlitequery = "insert images (url, image) values (?, ?)"; sqlite3_stmt* statement; if( sqlite3_prepare_v2(articlesdb, sqlitequery, -1, &statement, null) == sqlite_ok ) { sqlite3_bind_text(statement, 1, [mainurl utf8string], -1, sqlite_transient); sqlite3_bind_blob(statement, 2, [imgdata bytes], [imgdata length], sqlite_transient); sqlite3_step(statement); } else nslog( @"savebody: failed sqlite3_prepare_v2. error is: %s", sqlite3_errmsg(articlesdb) ); // finalize , close database. sqlite3_finalize(statement); }
and here function load images:
// load images data base given image url - (nsdata*) loadimagesfromsql: (nsstring*) imagelink { nsdata* data = nil; nsstring* sqlitequery = [nsstring stringwithformat:@"select image images url = '%@'", imagelink]; sqlite3_stmt* statement; if( sqlite3_prepare_v2(articlesdb, [sqlitequery utf8string], -1, &statement, null) == sqlite_ok ) { if( sqlite3_step(statement) == sqlite_row ) { int length = sqlite3_column_bytes(statement, 0); data = [nsdata datawithbytes:sqlite3_column_blob(statement, 0) length:length]; } } // finalize , close database. sqlite3_finalize(statement); return data; }
Comments
Post a Comment