gpt4 book ai didi

ios - 试图将图像存储到 sqlite 中,当我尝试检索它时,它是一个字节而不是 200 万字节

转载 作者:行者123 更新时间:2023-11-28 19:38:53 26 4
gpt4 key购买 nike

当我尝试将图像存储到 sqlite 中时,我已将该图像转换为 NSData,并且 imageData 中有超过 2,000,000 字节。

当我尝试从 sqlite 数据库中检索它时,它变成了一个字节。

这是我的代码:

将图片插入sqlite:

    -(void)INSERTCLAIMATTACHMENT:(NSString *)tablename
PARENTID:(long long)parentid
ATTACHMENTCONTENT:(UIImage *)attachmentimage
ATTACHMENTNAME:(NSString *)attachmentname
{
sqlite3_stmt *statement;
const char *dbPath = [_databasePath UTF8String];

Global * g = [[Global alloc]init];
NSData *imageData = [[NSData alloc]init];
imageData = UIImagePNGRepresentation(attachmentimage);

if(sqlite3_open(dbPath, &_DB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO CLAIM_ATTACHMENT_ITEM ('PARENT_ID', 'CLAIM_ATTACHMENT_CONTENT', 'CLAIM_ATTACHMENT_NAME', 'CLAIM_ATTACHMENT_SIZE') VALUES ('%lld', '?', '%@', '%lu')", parentid,attachmentname, [imageData length]];
const char *insert_statement = [insertSQL UTF8String];

if(sqlite3_prepare_v2(_DB,insert_statement, -1, &statement, NULL) == SQLITE_OK)
{
sqlite3_bind_blob(statement, 2, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
if(sqlite3_step(statement) == SQLITE_DONE)
{
[g showCustomAlertView:@"Successfull" message:@"Attachment has been added into database."];
}
else{
[g showCustomAlertView:@"Failure" message:[NSString stringWithFormat:@"%s", sqlite3_errmsg(_DB)]];
}

}
else{
[g showCustomAlertView:@"Failure" message:[NSString stringWithFormat:@"%s", sqlite3_errmsg(_DB)]];
}

}
else{
[g showCustomAlertView:@"Failure" message:[NSString stringWithFormat:@"%s", sqlite3_errmsg(_DB)]];
}
sqlite3_close(_DB);
}

从sqlite中获取图片

-(NSMutableArray *)SELECTATTACHMENTFROMCLAIMATTACHMENTITEM:(long long)rowID
{
sqlite3_stmt *statement;
const char *dbPath = [_databasePath UTF8String];

Global * g = [[Global alloc]init];

NSMutableArray *attachmentArray = [[NSMutableArray alloc]init];

if(sqlite3_open(dbPath, &_DB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM CLAIM_ATTACHMENT_ITEM WHERE PARENT_ID = '%lld'", rowID];
const char *query_Statement = [querySQL UTF8String];

if(sqlite3_prepare_v2(_DB, query_Statement, -1, &statement, NULL) == SQLITE_OK)
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
AttachmentItem * attachment = [[AttachmentItem alloc]init];
NSData *attachmentImageData = [[NSData alloc]initWithBytes:sqlite3_column_blob(statement, 2) length:sqlite3_column_bytes(statement, 2)];

UIImage *attachmentImage = [UIImage imageWithData:attachmentImageData];

NSString *attachmentName = [[NSString alloc]initWithUTF8String:sqlite3_column_text(statement, 3)];
attachment.attachmentImage = attachmentImage;
attachment.attachmentName = attachmentName;
[attachmentArray addObject:attachment];
}
}
else
{
[g showCustomAlertView:@"Failure" message:[NSString stringWithFormat:@"%s", sqlite3_errmsg(_DB)]];
}
}
else
{
[g showCustomAlertView:@"Failure" message:[NSString stringWithFormat: @"%s",sqlite3_errmsg(_DB)]];
}


sqlite3_close(_DB);

return attachmentArray;

}

发生了什么?为什么图像数据对我来说缩成了一个字节:(我猜 blob 可能无法存储那么大的数据?

最佳答案

您对 sqlite_bind_blob 的调用传递了错误的字段索引。 INSERT 语句中只有一个 ?,因此索引应该是 1,而不是 2

sqlite3_bind_blob(statement, 1, [imageData bytes], [imageData length], SQLITE_TRANSIENT);

此外,您需要从 INSERT 语句中的 ? 中删除引号:

NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO CLAIM_ATTACHMENT_ITEM ('PARENT_ID', 'CLAIM_ATTACHMENT_CONTENT', 'CLAIM_ATTACHMENT_NAME', 'CLAIM_ATTACHMENT_SIZE') VALUES ('%lld', ?, '%@', '%lu')", parentid,attachmentname, [imageData length]];

使用 ? 结合适当的 sqlite3_bind_xxx 语句而不是使用 stringWithFormat: 构建查询的主要原因是值是正确的为你引用转义。

关于ios - 试图将图像存储到 sqlite 中,当我尝试检索它时,它是一个字节而不是 200 万字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36169711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com