gpt4 book ai didi

iphone - 将图像存储和检索到 iphone 的 sqlite 数据库中

转载 作者:行者123 更新时间:2023-11-29 13:42:17 25 4
gpt4 key购买 nike

我正在尝试从 sqlite 数据库存储和检索图像和文本..我的 saveDatabase 编码工作正常..但我不知道它是否保存在我的数据库中..这是我的代码..

    sqlite3 *database;
dbName=@"dataTable.sqlite";
NSArray *documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentdir=[documentpath objectAtIndex:0];
dbPath=[documentdir stringByAppendingPathComponent:dbName];
sqlite3_stmt *compiledStmt;
if(sqlite3_open([dbPath UTF8String], &database)==SQLITE_OK){
NSLog(@"Name:%@,Company:%@,URL:%@",model.personName,model.companyName,model.imgurl);
// NSString *sqlStatement=[NSString stringWithFormat:@"insert or ignore into Persons(PersonName,CompanyName,ImgUrl)values(\"%@\",\"%@\",\"%@\")",model.personName,model.companyName,model.imgurl];
// const char *insertSQL=[sqlStatement UTF8String];

const char *insertSQL="insert or ignore into Persons(PersonName,CompanyName,ImgUrl,UserImage)values(?,?,?,?)";
sqlite3_bind_text(compiledStmt,1,[model.personName UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStmt,2,[model.companyName UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStmt,3,[model.imgurl UTF8String],-1,SQLITE_TRANSIENT);
NSData *imageData=UIImagePNGRepresentation(imageView.image);
sqlite3_bind_blob(compiledStmt, 4, [imageData bytes], [imageData length], NULL);
if(sqlite3_prepare_v2(database,insertSQL, -1, &compiledStmt, NULL)==SQLITE_OK){
sqlite3_step(compiledStmt);

// char *errMsg;
// sqlite3_exec(database, insertSQL, NULL,compiledStmt,&errMsg);
NSLog(@"Add to Favourites");

当我尝试检索我的数据库时出现错误..

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'

这是我读取数据库的代码..

    sqlite3 *database;

favArray=[[NSMutableArray alloc]init];
if(sqlite3_open([dbPath UTF8String],&database)==SQLITE_OK)
{
const char *sqlStatement="select * from Persons";
sqlite3_stmt *compiledStatement;

if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)==SQLITE_OK)
{
while (sqlite3_step(compiledStatement)==SQLITE_ROW)
{
int personId = sqlite3_column_int(compiledStatement,0);

NSString *personName=[NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 1)];

NSString *companyName=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

NSString *imgurl=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
int length = sqlite3_column_bytes(compiledStatement, 4);
// NSData *data = [NSData dataWithBytes:sqlite3_column_blob(compiledStatement, 4) length:length];
NSString *imageString=[NSString stringWithCharacters:sqlite3_column_blob(compiledStatement, 4) length:length];
UIImage *personImage=[UIImage imageNamed:imageString];

Favourites *favourites=[[Favourites alloc]initWithPersonId:personId personName:personName companyName:companyName imgurl:imgurl personImage:personImage];
[favArray addObject:favourites];
}
}sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);

我认为我在通过错误的数据访问方式读取数据库时做错了..任何人都可以帮助我从数据库中获取图像...

最佳答案

最后我发现我在哪个地方遇到了问题...首先我检查了数据存储在我的模拟器数据库中..它没有存储.所以我发现我的插入编码有问题...这是我m 在 prepare 语句之前绑定(bind)数据...我找到的答案是..

if(sqlite3_prepare_v2(database,insertSQL, -1, &compiledStmt, NULL)==SQLITE_OK){

sqlite3_bind_text(compiledStmt,1,[model.personName UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStmt,2,[model.companyName UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStmt,3,[model.imgurl UTF8String],-1,SQLITE_TRANSIENT);
NSData *imageData=UIImagePNGRepresentation(imageView.image);
sqlite3_bind_blob(compiledStmt, 4, [imageData bytes], [imageData length], NULL);

sqlite3_step(compiledStmt);

// char *errMsg;
// sqlite3_exec(database, insertSQL, NULL,compiledStmt,&errMsg);
NSLog(@"Add to Favourites");
}

关于iphone - 将图像存储和检索到 iphone 的 sqlite 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8728834/

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