On Fri, 17 Oct 2008 07:53:52 +0000 (UTC), MikeW
<[EMAIL PROTECTED]> wrote:

>John Belli <[EMAIL PROTECTED]> writes:
>> 
>> On Thu, 16 Oct 2008 17:54:52 +0000 (UTC), MikeW
>> <[EMAIL PROTECTED]> wrote:
>> 
>> >My code has two pre-opened DB connection handles, and would like to
>> >perform an SQL operation over the two databases.
>> >The DB filenames are not available at this point, hence an ATTACH is
>> >not feasible.
>> >Is there any way to achieve this by applying an ATTACH at the handle level ?
>> 
>> I don't think so, but check into "PRAGMA database_list" (See
>> http://www.sqlite.org/pragma.html ).
>> 
>> JAB
>
>Thanks, that might be useful in some situations but not in this one.
>
>The use case is a very specific one in which a database may contain one-off
>overrides for a second, normally independent (but already opened) database.
>I can obviously implement this by reading rows from the first one
>and updating the second, but if I could briefly refer to both DBs at
>the same time, it would be just one 'atomic' SQL statement -
>much simpler and more robust.
>
>Still, it seems surprising that there isn't a (SQLite-specific) handle-level
>operation to do this attach, maybe I should put in a feature request.
>(Or even have a look in the SQLite code to see if it's an easy mod ...)

GIVEN: db1 is the open handle of the first database, and db2 is the
open handle of the second database

WANTED: attach database open as db1 to db2 to do an atomic update
 * * * WARNING: untested code ahead! * * *
char **table;
sqlite3_get_table(db1, "PRAGMA database_list", &table, NULL, NULL,
                  NULL);
char *tmp = sqlite3_mprintf("ATTACH %q AS a", table[5]);
sqlite3_free_table(table);
sqlite3_exec(db2, tmp, NULL, NULL, NULL);
sqlite3_free(tmp);
   ...do your stuff here...
sqlite3_exec(db2, "DETACH a", NULL, NULL, NULL);

There should be a bunch of error checking in there. This is left as an
exercise for the reader.

This will not work if db1 is a memory db. In that case, you would have
to do it the other way around. If both are memory dbs, then you're out
of luck.


JAB
-- 
John A. Belli
Software Engineer
Refrigerated Transport Electronics, Inc.
http://www.rtelectronics.com

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to