如何在iPhone应用程序开发中运行多个SQlite查询

时间:2012-05-10 08:55:49

标签: iphone objective-c ios ipad sqlite

我正在开发一个应用程序,我希望从SQlite中的多个表中获取数据。

-(void)checkAndCreateDatabase{
    BOOL success;
    NSFileManager *fileManager=[NSFileManager defaultManager];
    success=[fileManager fileExistsAtPath:databasePath];
    if(success)
        return;

    NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
-(void)readData{
    sqlite3 *database;
    genderList=[[NSMutableArray alloc]init];
    if(sqlite3_open([databasePath UTF8String], &database)== SQLITE_OK){
        const char *sqlStatement = "select name from gender";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)==SQLITE_OK){
            while (sqlite3_step(compiledStatement)==SQLITE_ROW) {
                NSInteger pId = sqlite3_column_int(compiledStatement, 0);
                NSString *stringName=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                db *info =[[db alloc]initWithID:pId andName:stringName];
                [genderList addObject:info];
            }            
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}

在上面的代码中,我从性别中传递了一个Query Select名称。同时我想传递从内容中选择名称并将其保存在不同的数组中。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以使用同样明智的代码

-(NSUInteger)getCounter:(NSUInteger)id{
    NSUInteger final=0;

    NSMutableArray *ar=[[NSMutableArray alloc] init];
    NSString *strQuery=[NSString stringWithFormat:@"select qty from Datatable where id=?"];
    const char *query=[strQuery UTF8String];
    sqlite3_stmt *compiledStmt;
    if(sqlite3_open([self.dbPath UTF8String], &database)==SQLITE_OK) {
        if(sqlite3_prepare_v2(database, query, -1, &compiledStmt, NULL)==SQLITE_OK){
            sqlite3_bind_int(compiledStmt, 1, id);
            while (sqlite3_step(compiledStmt)==SQLITE_ROW) {
                GET_Integer(qty,0);
                final=qty;
                [ar addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:qty],@"qty",nil]];
            }
        } else {
            NSLog(@"Invalid query");
        }
    } else {
        NSLog(@"error while opening database.");
    }

    return final;
}

对整数

使用内联函数
#define GET_Integer(_TO_,_FROM_) NSUInteger _TO_; { \
_TO_ = sqlite3_column_int(compiledStmt,_FROM_); \
}

还有更多用途

#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_); \
}