0

我想将 iOS 的数据保存NSData到 的数据库中FMDB,例如:

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

但它不起作用。

4

2 回答 2

1

像这样试试

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

参考:sqlite3 在数据库中插入和读取 BLOB 数据

于 2013-04-05T07:24:14.960 回答
0

确保您完成了所有这些步骤。

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];
于 2013-04-05T07:27:58.457 回答