iphone - Save blob'type in database -
i want save data of ios, nsdata
database of fmdb
, such as:
nsurl *url=[nsurlurlwithstring:@"http://pic17.nipic.com/20111023/7050178_121536234000_2.jpg"]; nsdata *date=[nsdata datawithcontentsofurl:url]; nsstring *img=[nsstring stringwithformat:@"insert name(picture) values('%@')",date ]; bool judge1=[self.database executeupdate:img];
but doesn't work.
try this,
nslog(@"\ncreating db"); nsstring *str = @"create table if not exists images (image1 blob);"; int res = sqlite_error; res = sqlite3_open([@"aa.sql" utf8string], &database); res = sqlite3_exec(database, [str utf8string], null, null, null); sqlite3_stmt *updstmt =nil; const char *sql = "insert images (image1) values (?);"; res = sqlite3_prepare_v2(database, sql, -1, &updstmt, null); if(res!= sqlite_ok) { nslog(@"error while creating update statement:%s", sqlite3_errmsg(database)); } uiimage *img = [uiimage imagenamed: @"flower.png"]; nsdata *imagedata = [nsdata datawithdata: uiimagepngrepresentation(img)]; res = sqlite3_bind_blob(updstmt, 1, [imagedata bytes], [imagedata length] , sqlite_transient); if((res = sqlite3_step(updstmt)) != sqlite_done) { nslog(@"error while updating: %@", sqlite3_errmsg(database)); sqlite3_reset(updstmt); } res = sqlite3_reset(updstmt); res = sqlite3_close(database);
Comments
Post a Comment