Hello all,
I'm having some memory allocation difficulties while using the SQLite C
API. The goal of the function that is currently giving me issues is just
trying to read the column names from a table. I have been having other
memory allocation faults elsewhere, but I currently have it isolated to
this particular section of code (I say this because I suppose the root
problem could be elsewhere in my code). Also strangely enough, if I do not
store the data returned from sqlite3_column_name, but instead just print it
to stdout, I do not get a segmentation fault, which also leads to me
thinking my error may be more fundamental... This function is also run
multiple times and only produces segmentation faults on the third table I
run it on. I am using the database
here<http://download.codeplex.com/Download/Release?ProjectName=chinookdatabase&DownloadId=167067&FileTime=129342699263670000&Build=18924>for
testing.
Please let me know if I can provide any more relevant information.
Any help would be greatly appreciated. Below is the relevant code segment.
Thanks!
char **getColumnNames(char **columnNames, sqlite3 *database, char
*tableName) {
sqlite3_stmt *statement = NULL;
char sqlStatementString[100];
char *sqlErrorMessage = 0;
char buffer[100];
int status = 0;
int i = 0;
int numOfCols = 0;
// Prepare a statement to get the tables from the database
sprintf(sqlStatementString, "SELECT * FROM '%s';", tableName);
status = sqlite3_prepare_v2(database, sqlStatementString,
strlen(sqlStatementString), &statement, NULL);
if (status != SQLITE_OK) {
fprintf(stderr, "Error occured when attempting to prepare an SQL
statement\n"
"Error:%s\n", sqlErrorMessage);
// Prevents memory leaking from error message:
sqlite3_free(sqlErrorMessage);
exit(1);
} // end error check
// I only want the first row, because I am only getting the names
// for the columns. This is why I only run the step function once
if (sqlite3_step(statement) != SQLITE_ROW) {
fprintf(stderr, "Either an error occured or table \"%s\" has no
data!\n", tableName);
exit(1);
}
numOfCols = sqlite3_column_count(statement);
if (numOfCols < 1) {
perror("No columns in that table!\n");
exit(1);
}
columnNames = (char **) malloc(numOfCols * sizeof(char *));
if (columnNames == NULL) {
perror("Allocation error: ");
exit(1);
} // end error check for malloc
for (i = 0; i < numOfCols; i++) {
sprintf(buffer, "%s", (char *) sqlite3_column_name(statement, i));
columnNames[i] = malloc(strlen(buffer));
if (columnNames[i] == NULL) {
perror("Failed to allocate memory for a column name\n");
exit(1);
} // end malloc error check
strcpy(columnNames[i], buffer);
} // end column loop
// Destroy the statement
// ******** THIS IS WHERE I GET THE SEGMENTATION FAULT **********
status = sqlite3_finalize(statement);
if (status != SQLITE_OK) {
fprintf(stderr, "Error occured when attempting to destroy a SQL
statement\n"
"Error:%s\n", sqlErrorMessage);
// Prevents memory leaking from error message:
sqlite3_free(sqlErrorMessage);
exit(1);
} // end error check
return columnNames;
} // end of function
--
Stephen Wood
RMCI, INC.
1525 Perimeter Parkway
Suite 430
Huntsville, AL 35806**
**
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users