简体   繁体   English

ANSI C 和 sqlite3_get_table:如何将结果数组传递回调用 function?

[英]ANSI C and sqlite3_get_table: How to pass results array back to calling function?

I'm trying to pass the results array generated by sqlite3_get_table contained in a function called by another function.我正在尝试传递由另一个 function 调用的 function 中包含的sqlite3_get_table生成的结果数组。 Everything is working fine except I can't get the array back into the main function.一切工作正常,除了我无法将阵列放回main function。 What exactly do I replace the char **sql_results with?我到底用什么替换char **sql_results Here's the code:这是代码:

int main()
{
    char **sql_results = 0; // help here

    sql_select_table("GetAirports", query_string, AIRPORTS_DATABASE,
             sql_results, &RecordCount,
             &ColumnCount);

    for (int i = 0; i < ColumnCount; i++)
        printf("%s\n", sql_results[i]); // Generates "EXC_BAD_ACCESS"

}

void sql_select_table(const char *query_name, const char *sql_query,
              const char *sql_database,
              char **sql_results, int *RecordCount,
              int *ColumnCount)
{
    sqlite3_get_table(open_database, sql_query,
                 &sql_results, RecordCount,
                 ColumnCount, &error_msg);

}

You're trying to take the address of one of the sql_select_table arguments and that doesn't do what you think it does;您正在尝试获取sql_select_table arguments 之一的地址,但这并没有按照您的想法进行; &sql_results is just the address of a local variable, it won't let you modify the sql_results in main() . &sql_results只是一个局部变量的地址,它不会让你修改main()中的sql_results

Instead, pass a char*** into sql_select_table like this:相反,将char***传递到sql_select_table ,如下所示:

/* Note that sql_results is now a char*** */
void sql_select_table(const char *query_name, const char *sql_query,
              const char *sql_database,
              char ***sql_results, int *RecordCount,
              int *ColumnCount)
{
    sqlite3_get_table(open_database, sql_query,
                 sql_results, RecordCount,
                 ColumnCount, &error_msg);

}

int main()
{
    char **sql_results = 0;

    /* Note the & on sql_results */
    sql_select_table("GetAirports", query_string, AIRPORTS_DATABASE,
             &sql_results, &RecordCount,
             &ColumnCount);

    for (int i = 0; i < ColumnCount; i++)
        printf("%s\n", sql_results[i]); // Generates "EXC_BAD_ACCESS"

}

Also, sqlite3_get_table is deprecated:此外,不推荐使用sqlite3_get_table

This is a legacy interface that is preserved for backwards compatibility.这是为向后兼容而保留的遗留接口。 Use of this interface is not recommended.不推荐使用此接口。

So you might want to start using sqlite3_exec instead.所以你可能想开始使用sqlite3_exec

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM