gpt4 book ai didi

ios - 图片未存储在sqlite中

转载 作者:行者123 更新时间:2023-12-03 18:33:41 25 4
gpt4 key购买 nike

我做了一个演示,将图像存储到数据库。目前我没有任何错误,但是我的图像没有存储在sqlite数据库中。
请查看以下代码,并告诉我我的错误在哪里。

DBManager类

sqlite3 *sqlite3DatabaseObject;
sqlite3_stmt* sqlite3Statement;
-(void)database
{

directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [directoryPaths objectAtIndex:0];
NSString * databasePath = [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:@"informationdb.sql"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:databasePath] == NO) {
const char *dbPath = [databasePath UTF8String];
if (sqlite3_open(dbPath, &sqlite3DatabaseObject)== SQLITE_OK) {
char * errorMessage;
const char *sqlite3Query = "CREATE TABLE IF NOT EXISTS PICTURES (PHOTO BLOB)";
if (sqlite3_exec(sqlite3DatabaseObject, sqlite3Query, NULL, NULL, &errorMessage)!= SQLITE_OK) {
NSLog(@"failed = %@",sqlite3DatabaseObject);
}
sqlite3_close(sqlite3DatabaseObject);
}else{
NSLog(@"failed to create database");
}
}
}
- (void) SaveImagesToSql: (NSData*) imgData {

NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"informationdb" ofType:@"sql"];

const char* sqlite3Query = "INSERT INTO PICTURES (PHOTO) VALUES (?)";
int openDatabaseResult = sqlite3_open_v2([databasePath UTF8String], &sqlite3DatabaseObject, SQLITE_OPEN_READWRITE , NULL);
if (openDatabaseResult == SQLITE_OK) {
int sqlite3Prepare = sqlite3_prepare_v2(sqlite3DatabaseObject, sqlite3Query, -1, &sqlite3Statement, NULL);
if( sqlite3Prepare == SQLITE_OK ) {
sqlite3_bind_blob(sqlite3Statement, 1, [imgData bytes], [imgData length], SQLITE_TRANSIENT);
sqlite3_step(sqlite3Statement);
}
else
{
NSLog(@"Error is: %s", sqlite3_errmsg(sqlite3DatabaseObject));
}
sqlite3_finalize(sqlite3Statement);

}
else NSLog( @"Error is: %s", sqlite3_errmsg(sqlite3DatabaseObject) );
sqlite3_close(sqlite3DatabaseObject);
}

ViewController类
- (void)viewDidLoad {
[super viewDidLoad];
DBManager * dbmClass = [[DBManager alloc]init];
[dbmClass database];

imgView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(uploadOrCapture)];
[singleTap setNumberOfTapsRequired:1];
[imgView addGestureRecognizer:singleTap];

}
- (IBAction)btnSave:(id)sender {
DBManager*dbmClass = [[DBManager alloc]init];
NSData *imageInData = UIImagePNGRepresentation(imgView.image);
[dbmClass SaveImagesToSql:imageInData];
[self dismissViewControllerAnimated:YES completion:nil];
}

我无法找出问题所在。它成功执行了所有代码,但是当我从设备下载数据库并进行检查时,没有存储图像数据。

提前致谢。
米希尔

最佳答案

只需将此函数替换为我的代码即可,因为数据库路径错误

- (void) SaveImagesToSql: (NSData*) imgData  {

directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [directoryPaths objectAtIndex:0];
NSString * databasePath = [[NSString alloc]initWithString:[documentsDirectory stringByAppendingPathComponent:@"informationdb.sql"]];

const char* sqlite3Query = "INSERT INTO PICTURES (PHOTO) VALUES (?)";
int openDatabaseResult = sqlite3_open_v2([databasePath UTF8String], &sqlite3DatabaseObject, SQLITE_OPEN_READWRITE , NULL);
if (openDatabaseResult == SQLITE_OK) {
int sqlite3Prepare = sqlite3_prepare_v2(sqlite3DatabaseObject, sqlite3Query, -1, &sqlite3Statement, NULL);
if( sqlite3Prepare == SQLITE_OK ) {
sqlite3_bind_blob(sqlite3Statement, 1, [imgData bytes], [imgData length], SQLITE_TRANSIENT);
sqlite3_step(sqlite3Statement);
}
else
{
NSLog(@"Error is: %s", sqlite3_errmsg(sqlite3DatabaseObject));
}
sqlite3_finalize(sqlite3Statement);

}
else NSLog( @"Error is: %s", sqlite3_errmsg(sqlite3DatabaseObject) );
sqlite3_close(sqlite3DatabaseObject);
}

这是数据库截图

enter image description here

不要拖到您项目中的数据库。

关于ios - 图片未存储在sqlite中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43339680/

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