tags:

views:

625

answers:

2

I'm using the sqlite C API.
"Everything" is run in the main function (variables, open, execute a query, close the database, etc.) In addition, I would like to now create separate user defined functions where I do, for example, a sort on the database.

What is the best method to pass the db objects and variables from main to this new function? Create them all as globals? Pass by reference? Is this what the Sqlite3_create_function is for?

Please let me know how I can clarify this question if needed.

Thanks.

+1  A: 

sqlite3_create_function takes a void *pApp argument. The intention is that this points to data for use by your function. The pointer is stored in the SQLite managed function context. You get this pointer back from within the user function using the sqlite3_user_data. Since you allocate the memory, you can share it between your application and the function.

This mechanism isn't much harder than using globals, and supports multi-threaded apps if you decde to do that later.

Doug Currie
+1  A: 

Pass the sqlite3 *db pointer as argument to your function.

Or use callbacks as seen in the quickstart guide

kazanaki