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. Everything is working fine except I
can't get the array back into the main
function. What exactly do I replace the
char **sql_results
with? 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_results
is just the address of a local variable, it won't let you modify the sql_results
in main()
.
Instead, pass a char***
into sql_select_table
like this:
/* 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:
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.
精彩评论