0

I want save data of iOS, NSData to the 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 into name(picture) values('%@')",date ];
BOOL judge1=[self.database executeUpdate:img];

But it doesn't work.

5
  • are you getting any error or exception?
    – iEngineer
    Apr 5, 2013 at 7:10
  • saving image data in database is not fair thing,because it takes much size better you can save path of the image in DB.
    – jkk
    Apr 5, 2013 at 7:18
  • don't get! but don't display
    – willBetter
    Apr 5, 2013 at 7:18
  • I want to change imgae to data of ios'NSData,then save the data,It's right???
    – willBetter
    Apr 5, 2013 at 7:25
  • As @Mak said, you should save image locally in your application and then just save the path of that image in database, not whole image data.
    – iEngineer
    Apr 5, 2013 at 7:33

2 Answers 2

1

Try like 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 INTO 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);

Refer : sqlite3 insert and read BLOB data in database

0

Make sure you did all these steps.

FMDatabase *databse = [FMDatabase databaseWithPath:@"Path"];
[database open];  
NSURL *url=[NSURLURLWithString:@"http://pic17.nipic.com/20111023/7050178_121536234000_2.jpg"];  
NSData *date=[NSData dataWithContentsOfURL:url];  
NSString *img=[NSString stringWithFormat:@"INSERT INTO name (picture) values('%@')",date ];  
BOOL judge1;  
judge1=[database executeUpdate:img];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.