我想在sqlite3中插入一个图像,我使用“create table table1(photo blob,name varchar(10));”创建了表格。“现在我想在其中插入一个图像,那么插入图像的语法是什么?在另一个表中,我想存储图像的位置,因此对于第二个表,我的创建表和插入表语法是什么。你能帮我吗???? 如何从命令行检查它是否正确插入?
答案 0 :(得分:3)
您可以通过以下代码行在sqlite3中插入图像。
1)=首先准备SQLite插入语句
static sqlite3_stmt *insert_statement; //Declare Insert statement
- (void)InsertIntoDatabase{ // start insert method
const char *sql = "INSERT INTO <yourtable> (<column1>, <BLOBColumnName>) VALUES (? , ?)" ;
if(sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK)
NSAssert1(0, @"Error while creating insert statement. '%s'", sqlite3_errmsg(database));}
//For simple integer column**
sqlite3_bind_int(insert_statement, 1, <value column 1>);
//For image column**
NSString *webUrl = @"http://yourwebsite/youImage.jpg";
UIImage *myImage = [self GetRawImage:webUrl]; //Get image from method below
if(myImage != nil){
NSData *imgData = UIImagePNGRepresentation(myImage);
sqlite3_bind_blob(insert_statement, 2, [imgData bytes], [imgData length], NULL);
}
else {
sqlite3_bind_blob(insert_statement, 2, nil, -1, NULL);
}
} // end of insert method
2)=获取原始图像文件的方法。
- (UIImage *)GetRawImage:(NSString *)imageUrlPath
{
NSURL *imageURL = [NSURL URLWithString:imageUrlPath];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];
return image;
}
!它做得很简单
答案 1 :(得分:1)
为什么不简单地将图像保存在iphone文件系统上并仅在sqlite中保存其位置信息?
答案 2 :(得分:1)
您可以获取图像的原始NSData并保存...
NSData *imageData = UIImagePNGRepresentation(image);