gpt4 book ai didi

iphone - iPhone 中的存储

转载 作者:搜寻专家 更新时间:2023-10-30 23:18:51 24 4
gpt4 key购买 nike

我需要开发一个应用程序,我需要在其中将产品存储到购物车中。此外,我还需要以最后一次排序的方式检索最近的订单。
这是存储数据的最佳方式 - 使用plist 还是 SQLite 数据库?

最佳答案

我创建了那种类型的应用程序,在其中我使用 Sqlite 数据库来实现易于操作而不是 .plist 文件。

下面是一些将项目插入数据库的代码。

初始化数据库

-(id)init{
if(self=[super init]) {
documentsDirectory_Statement;
documentsDirectory=[documentsDirectory stringByAppendingPathComponent:@"DietCenter.rsd"];
self.dbPath=documentsDirectory;
NSFileManager *fm=[NSFileManager defaultManager];
if(![fm fileExistsAtPath:self.dbPath]) {
NSString *localDB=[[NSBundle mainBundle] pathForResource:@"DietCenter" ofType:@"rsd"];
NSError *err;
if(![fm copyItemAtPath:localDB toPath:self.dbPath error:&err]){
NSLog(@"Error in creating DB -> %@",err);
}
}

if(sqlite3_open([self.dbPath UTF8String], &database) !=SQLITE_OK){
NSLog(@"error while opening database.");
} else {
sqlite3_close(database);
}
}
return self;
}

同意值

  #define PUT_Value(_TO_,_FROM_) { \
NSString *str=[dObj valueForKey:_FROM_]; \
if(!str) str=@" "; \
sqlite3_bind_text(compiledStmt,_TO_,[str UTF8String],-1,SQLITE_TRANSIENT); \
}

#define PUT_Integer(_TO_,_FROM_) { \
NSInteger numbr=[[dObj valueForKey:_FROM_] intValue]; \
sqlite3_bind_int(compiledStmt,_TO_,numbr); \
}


#define PUT_Double(_TO_,_FROM_) { \
CGFloat doubleX=[[dObj valueForKey:_FROM_] floatValue]; \
sqlite3_bind_double(compiledStmt,_TO_,doubleX); \
}



#define GET_Value(_TO_,_FROM_) NSString *_TO_; { \
const unsigned char *c=sqlite3_column_text(compiledStmt,_FROM_); \
_TO_= c ? [NSString stringWithUTF8String:(char*)c] : @"" ; \
}

#define GET_Double(_TO_,_FROM_) double _TO_;{ \
_TO_=sqlite3_column_double(compiledStmt,_FROM_); \
}

#define GET_Integer(_TO_,_FROM_) NSUInteger _TO_; { \
_TO_ = sqlite3_column_int(compiledStmt,_FROM_); \
}

插入值函数 -(void)insertItem:(NSArray*)arItem{

    sqlite3_stmt *compiledStmt;
if(sqlite3_open([self.dbPath UTF8String], &database) ==SQLITE_OK) {


sqlite3_prepare_v2(database, "BEGIN TRANSACTION", -1, &compiledStmt, NULL);
sqlite3_step(compiledStmt);
sqlite3_finalize(compiledStmt);

const char *sqlInsertQry="insert into CartDetails (id,cat_id,KD,item,qty,calories,ar_name) values(?,?,?,?,?,?,?)";
if(sqlite3_prepare_v2(database, sqlInsertQry, -1, &compiledStmt, NULL) == SQLITE_OK ){
for (NSDictionary *dObj in arItem) {
PUT_Integer(1,@"id");
PUT_Integer(2,@"Cat_Id");
PUT_Double(3,@"KD");
PUT_Value(4,@"item");
PUT_Integer(5,@"qty");
PUT_Value(6,@"calories");
PUT_Value(7,@"ar_name");

NSUInteger err = sqlite3_step(compiledStmt);
if (err != SQLITE_DONE){
NSLog(@"error while binding %d %s",err, sqlite3_errmsg(database));
}
sqlite3_reset(compiledStmt);
}
sqlite3_finalize(compiledStmt);
} else {
NSLog(@"Invalid Query");
}
sqlite3_prepare_v2(database, "END TRANSACTION", -1, &compiledStmt, NULL);
sqlite3_step(compiledStmt);
sqlite3_finalize(compiledStmt);
sqlite3_close(database);
} else {
NSLog(@"error while opening database.");
}
}

如果您对此有任何疑问,请告诉我我可以帮助您

感谢和问候,

@塞缪尔

关于iphone - iPhone 中的存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9288361/

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