4

我很难在网上找到一个关于使用 xcode 和 cocos2dx 从 SQlite DB 获取多个值的工作示例。这是我的sql查询:

char sql_query[100];
sprintf(sql_query, "SELECT * FROM SQList WHERE ColumnD BETWEEN %d AND %d ORDER BY RANDOM() LIMIT 1", MinColumnD, MaxColumnD);

它本身的查询似乎有效,主要问题是如何将我从“select *”收集的值获取到另一个 int 或 char 参数中以便我可以使用它?

我发现一些示例提到了使用对结构的回调或提到使用 sqlite3_prepare_v2 和 step 方法。

不过,我无法找到这两种方法的示例,请帮忙!

4

2 回答 2

9

使用时sqlite3_exec,你必须转换字符串中的所有值,并且你必须使用回调的void *指针或一些全局变量来返回数据:

struct MyData {
    string A;
    int B, C;
};

int exec_callback(void *ptr, int argc, char *argv[], char *names[])
{
    vector<MyData> *list = reinterpret_cast<vector<MyData> *>(ptr);
    MyData d;
    d.A = argv[0] ? argv[0] : "";
    d.B = atoi(argv[1]);
    d.C = atoi(argv[2]);
    list->push_back(d);
    return 0;
}

void query_with_exec()
{
    vector<MyData> list;

    char *errmsg = NULL;
    sqlite3_exec(db, "SELECT a, b, c FROM SQList /* WHERE ... */",
                 exec_callback, &list, &errmsg);
    if (errmsg) {
        printf("error: %s!\n", errmsg);
        return;
    }

    // use list ...
}

使用时sqlite3_prepare*,您必须sqlite3_step循环调用,直到它不再返回SQLITE_ROW(当您期望只有一条记录时,您只能调用一次):

void query_with_step()
{
    vector<MyData> list;
    sqlite3_stmt *stmt;

    int rc = sqlite3_prepare_v2(db, "SELECT a, b, c FROM SQList /* WHERE ... */",
                                -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        printf("error: %s!\n", sqlite3_errmsg(db));
        return;
    }

    for (;;) {
        rc = sqlite3_step(stmt);
        if (rc == SQLITE_DONE)
            break;
        if (rc != SQLITE_ROW) {
            printf("error: %s!\n", sqlite3_errmsg(db));
            break;
        }

        MyData d;
        const char *text = (const char *)sqlite3_column_text(stmt, 0);
        d.A = text ? text : "";
        d.B = sqlite3_column_int(stmt, 1);
        d.C = sqlite3_column_int(stmt, 2);
        list.push_back(d);
    }

    sqlite3_finalize(stmt);

    // use list ...    
}
于 2012-11-24T10:53:01.557 回答
-1

使用“SELECT”查询获取值并将您的 char 和 int 值存储在一个数组中。创建一个 NSMUtableArray 并存储您将从 SELECT 查询中获得的值。以下代码将帮助您

-(int)find_putts_count:(int)holeno{
  sqlite3 *database;
 NSString *databasePath;
 NSMUtableArray *arr_tracking_details = [[NSMUtableArray alloc] init];
 int putts = -1;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:@"GolfElite.sqlite"];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
    // Setup the SQL Statement and compile it for faster access
    NSString *sqlStatement = nil;
            sqlStatement = [NSString stringWithFormat:@"select Putts from Holes WHERE HoleNo == %d and ShotNo == %d and PlayerId == '%@'",holeno,1,[arr_players_id objectAtIndex:scorecard_player_no]];
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) 
    {
        if(sqlite3_step(compiledStatement) == SQLITE_ROW)   
        {
            [arr_tracking_details addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]];
             [arr_tracking_details addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,6)]];
            [arr_tracking_details addObject:[NSString stringWithFormat:@"%d",(int)sqlite3_column_int(compiledStatement, 4)]];

            putts = (int)sqlite3_column_int(compiledStatement, 0);
        }
    }
    sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
return putts;
}
于 2012-11-24T05:44:19.460 回答