新手 sqlite3 存储图片的问题
小弟在做数据库存图片,网上搜了一下,但是没有达到目的。
贴代码;
- (NSString *) databasePath
{
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"path:%@",path);
return [path stringByAppendingPathComponent:@"test.sql"];
}
- (void) viewDidLoad {
[super viewDidLoad];
NSString *name = [self databasePath];
if (sqlite3_open([name UTF8String], &dataBase) != SQLITE_OK) {
NSLog(@"sql open error");
}
NSLog(@"sql create ok");
char *err;
NSString *createTableSQL = @"create table if not exists LOVE(image_ID integer primary key,url text,image BLOB)";
if (sqlite3_exec(dataBase, [createTableSQL UTF8String], NULL, NULL, &err) != SQLITE_OK) {
sqlite3_close(dataBase);
NSLog(@"create table error");
}
//cha
NSURL *url = [NSURL URLWithString:@"http://sh.centanet.com/ctPostImage/B4/51/C4AF8E4741D8A19DA1F081AB3BA1-9.jpg"];
NSString *urlString = [NSString stringWithFormat:@"%@",url];
NSData *imageData = [NSData dataWithContentsOfURL:url];
NSUInteger length = [imageData length];
sqlite3_stmt *statement;
NSString *query = [[NSString alloc] initWithString:[NSString stringWithFormat:
@"insert into LOVE(url,image),values(?,?)"]];
if (sqlite3_prepare_v2(dataBase, [query UTF8String], -1, &statement, NULL) != SQLITE_OK) {
NSLog(@"insert prepare error:");
return;
}
sqlite3_bind_text(statement, 1, [urlString UTF8String], strlen(urlString), SQLITE_TRANSIENT);
sqlite3_bind_blob(statement, 2, [imageData bytes], length, SQLITE_TRANSIENT);
sqlite3_step(statement);
//qu
NSString *selectSQL = @"select image from love";
if (sqlite3_prepare_v2(dataBase, [selectSQL UTF8String], -1, &statement, &err) == SQLITE_OK) {
NSLog(@"select sql pass");
while (sqlite3_step(statement)==SQLITE_ROW) {
NSData *imagDeata;
const void *test = sqlite3_column_blob(statement, 0);
int size = sqlite3_column_bytes(statement, 0);
imagDeata =[NSData dataWithBytes:test length:size];
UIImage *image1 = [UIImage imageWithData:imageData];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = image1;
[self.view addSubview:imageView];
[imageView release];
};
}
sqlite3_finalize(statement);
sqlite3_close(dataBase);
}
运行之后报错:insert prepare error
希望大虾能帮帮小弟