I have a structure declared like:
Code:
typedef struct _sqlite {
int rc;
int ncol;
int nrow;
char *db_name;
sqlite3 *db;
sqlite3_stmt *stmt;
char *query;
int db_status;
} SQL_handle;
I initialize the structure with this function:
Code:
int init_sql_handle(SQL_handle **handle)
{
SQL_handle *new;
new = safemalloc(sizeof(SQL_handle));
if(!new) {
fprintf(stderr, "Couldn't allocate sql init memory, terminating!\n");
exit(255);
}
new->query = safestrdup("SELECT main.id, artists.artist, main.title, main.remixer, \
labels.label, main.year, genres.genre, main.proj, main.mark, classes.class, \
main.class_no from main, artists, genres, labels, classes WHERE \
main.artist=artists.artist_id AND main.label=labels.label_id AND \
main.genre=genres.genre_id AND main.class=classes.class_id LIMIT 20;");
new->db_name = safestrdup("music.db");
new->nrow = 0;
new->rc = new->ncol = new->nrow = new->db_status = 0;
*handle = new;
}
My function to open the database with the structure is:
Code:
int open_db(SQL_handle **handle)
{
handle->rc = sqlite3_open(handle->db_name, &handle->db);
if(handle->rc) {
/* Print a pop-up screen for error. */
handle->db_status = close_db(handle->db);
exit(1);
}
return 0;
}
My function to prepare the db statement is:
Code:
int prepare_db(SQL_handle **handle)
{
const char *tail;
/* Does not affect the connection or database in any way.
* It does not start a transaction or get a lock.
* Ref: The Definitive Guide To SQL_handle, p. 216 */
handle->rc = sqlite3_prepare(handle->db, handle->query, strlen(handle->query), &handle->stmt, &tail);
if(handle->rc != SQLITE_OK) {
/* TODO: Handle error in db. */
exit(1);
}
return 0;
}
In main I call all three functions (in this order) like:
Code:
init_sql_handle(&handle);
open_db(&handle);
prepare_db(&handle);
...after creating the handle above main...
Code:
SQL_handle *handle = NULL;
When I compile, though, I get these errors:
Code:
gcc -g3 -c main.c
gcc -g3 -c color.c
gcc -g3 -c print.c
gcc -g3 -c tree.c
gcc -g3 -c window.c
gcc -g3 -c sql.c
sql.c: In function 'open_db':
sql.c:33: error: request for member 'rc' in something not a structure or union
sql.c:33: error: request for member 'db_name' in something not a structure or union
sql.c:33: error: request for member 'db' in something not a structure or union
sql.c:35: error: request for member 'rc' in something not a structure or union
sql.c:37: error: request for member 'db_status' in something not a structure or union
sql.c:37: error: request for member 'db' in something not a structure or union
sql.c: In function 'prepare_db':
sql.c:51: error: request for member 'rc' in something not a structure or union
sql.c:51: error: request for member 'db' in something not a structure or union
sql.c:51: error: request for member 'query' in something not a structure or union
sql.c:51: error: request for member 'query' in something not a structure or union
sql.c:51: error: request for member 'stmt' in something not a structure or union
sql.c:53: error: request for member 'rc' in something not a structure or union
make: *** [sql.o] Error 1
What am I doing wrong here??? I thought that I was to pass in a reference to the struct so that it could be modified. I could put these back into main but I wanted to use a function instead so I don't keep having to write the error checking.
Also, in prepare_db and open_db, in sqlite3_prepare and sqlite3_open, how would I pass those parameters that require a reference to stmt and db? I passed the structure as a reference anyways. Thank you.