小弟在做数据库存图片,网上搜了一下,但是没有达到目的。
贴代码;- (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希望大虾能帮帮小弟
贴代码;- (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希望大虾能帮帮小弟
解决方案 »
- 有哪位大神做过生成IOS授权文件啊,就是那个,"/AirFair/sync/afsync.rs"文件
- 为什么xcode的程序在touch上运行会黑屏
- 关于NSDate转NSString的问题
- 大家打括号的时候是一对一对打么?
- 点击tableview中的textField为什么没有出发 didSelectRowAtIndexPath事件?
- 问一个关于线程的问题
- 请教关于字符串转换float的问题
- Swift中NSURL转码问题
- iOS从webservice上请求数据,第一次接触怎么搞?
- 步伐企业版ipa,下载提示“无法下载应用程序,此时无法安装”
- 新手提问:断点调试如何进入函数内部调试?
- 如何判断Documents下的指定目录是否存在?
多占IO啊?移动设备更要效率!把URL保存在数据库,然后理顺管理逻辑就可以了。
非要那么办,你到官方找方案吧,毕竟开源的,还是C写的。
是这样的。一般都是把接收到的图片的 string 保存下来,再通过访问服务器来获取图片