Database Variable

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Database Variable

Gregory Moore-3
Hi

I sent this message previously, but it ended being a reply in another thread which was not my intention. So let’s try one more time...

I have an Objective-C iPhone application developed using Xcode. I have a singleton object which serves as the database controller. I use the SQLite C APIs to interact with the database. It works really well but I’m curious about something.

In my app, I declare my database instance variable like this. (It is a member of the DatabaseController class)

sqlite3 *_database;

My usage is like this:

int openCode = sqlite3_open(databasePathC, &_database);

I was poking around in FMDB’s source code on github. FMDB is a widely used Objective-C wrapper around SQLite. I was looking to see what they do out of curiosity.  FMDB declares and uses the database variable like this:

void *_database;

int openCode = sqlite3_open(databasePathC, (sqlite3**)&_database);

So my question is this: is there some advantage to declaring the database variable as a void pointer and casting it as sqlite3 verses just declaring it as sqlite3?

Thanks for creating SQLite!

Greg Moore
[hidden email]
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Database Variable

Clemens Ladisch
Gregory Moore wrote:
> void *_database;
>
> int openCode = sqlite3_open(databasePathC, (sqlite3**)&_database);
>
> is there some advantage to declaring the database variable as a void
> pointer and casting it as sqlite3 verses just declaring it as sqlite3?

Not really.  A plain void pointer does not require <sqlite3.h>, but then
you cannot do much with it, except copying it around.


Regards,
Clemens
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Database Variable

Jens Alfke-2
In reply to this post by Gregory Moore-3


> On Jan 2, 2018, at 1:49 PM, Gregory Moore <[hidden email]> wrote:
>
> I was poking around in FMDB’s source code on github. FMDB is a widely used Objective-C wrapper around SQLite. I was looking to see what they do out of curiosity.  FMDB declares and uses the database variable like this:
>
> void *_database;

This was probably just done to avoid having to include <sqlite3.h> in the FMDB header files.

—Jens
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Database Variable

Gregory Moore-3
Thanks for the reply. I see your point about avoiding including sqlite3.h in the header. I found the same can be accomplished by declaring a category in the implementation file and declaring the variable there as follows:

@interface DatabaseController()
{
     sqlite3 *_database;
}

@implementation
{
     <implementation code here>
}

In fact that’s the technique FMDB uses even though they still declare the variable as a void pointer. My apologies for neglecting to mention that.

Greg Moore
[hidden email]

> On Jan 2, 2018, at 4:51 PM, Jens Alfke <[hidden email]> wrote:
>
>
>
>> On Jan 2, 2018, at 1:49 PM, Gregory Moore <[hidden email]> wrote:
>>
>> I was poking around in FMDB’s source code on github. FMDB is a widely used Objective-C wrapper around SQLite. I was looking to see what they do out of curiosity.  FMDB declares and uses the database variable like this:
>>
>> void *_database;
>
> This was probably just done to avoid having to include <sqlite3.h> in the FMDB header files.
>
> —Jens
> _______________________________________________
> sqlite-users mailing list
> [hidden email]
> http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users