-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 07/28/2011 09:21 AM, Clifford Hung wrote:
> For statements, sqlite3_next_stmt() can be called to return a pointer to the 
> first prepared statement associated with a database connection 

In my opinion that API is a huge mistake outside the context of debugging tools.

C is a language where you have to manually keep track of the various items
you allocate and use.  There is no garbage collection and there is no
builtin way to enable reference counting.  There are no standard library
collections.  There is no tracking of memory ownership.

Consequently each piece of code/library/abstraction needs to look after
itself.  This API lets you reach in to something owned by some other piece
of code and unless that code was sloppily written you will pretty much only
end up harming it.

When you are writing short code, performing this housekeeping is a bit of a
pain, but remember that you don't actually have to close the statements and
database.  Providing you have committed you can exit the process and your
transactions will be durable.

If your code lives within a process that has a longer lifetime then you will
have to perform housekeeping because that is the C way.  There are numerous
wrappers binding the SQLite library to other programming languages and to my
knowledge almost all of them will perform the housekeeping for you that is
appropriate to the language.

If you will be using C then I recommend using one of the hierarchical memory
allocators.  What they let you do is provide a context for each memory
allocation including a destructor, and you can then free all of the relevant
allocations with one call.  As an example if you were handling requests you
could make each request be a context and all allocations relevant to that
request use that context.  At then end of handling that request, no matter
how many allocations happened or if there were errors, you can free
everything in one go.  (Note this all only works for allocations made using
the hierarchical memory allocator APIs.)

Here are two to get you started.  They are both open source and are licensed
in such a way that you can use them in a proprietary program:

  http://ccan.ozlabs.org/info/talloc.html
  http://swapped.cc/halloc/

Roger
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAk4xw74ACgkQmOOfHg372QQMfACfWfESN57aOkQN6HhfFoWNOhMs
i5gAnR9ACcxnuK7IoN7b9/jtcHgaRB4Z
=KXYW
-----END PGP SIGNATURE-----
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to