Re: [sqlite] ATTACH and sqlite3_open()

2007-05-10 Thread Dan Kennedy
On Wed, 2007-05-09 at 16:18 -0700, Jon Scully wrote:
> Simpler than that.  I merely want to attach to two databases (files).
> flash.db contains a set of tables that hold non-volatile data; ram.db
> contains a set of tables that is re-built (volatile) on re-boot -- but
> offers fast, read-only access.  No table-name overlaps, of course.
> 
> I want to access both sets of tables, seamlessly, as I should be able
> to do using ATTACH, at the command prompt, but do so using the C API.
> 
> Just wondering how others do this (Using ATTACH?  Using
> sqlite3_open()? Obviously I haven't looked very far into the
> sqlite3_open() code to see how it's put together, etc.).

Execute an ATTACH statement via sqlite3_exec(), or 
sqlite3_prepare/step/finalize.

> sqlite3 *db;
>
> if (sqlite3_open("flash.db", )) {
> fprintf(stderr, "Can't open the database in the Flash file
> system\n");
> exit(2);
> } else if (sqlite3_open("ram.db", )) {
> fprintf(stderr, "Can't open the database in the RAM-disk file
> system\n");
> sqlite3_close(db);
> exit(2);
> }

Don't do this. The second call to sqlite3_open opens a
new database connection to the file "ram.db" and overwrites
variable db with the new handle.

Dan.



-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] ATTACH and sqlite3_open()

2007-05-09 Thread Jon Scully

That worked as hoped. (Used your sqlite3_exec() version.)  Thanks.

On 5/9/07, Michael Ruck <[EMAIL PROTECTED]> wrote:

Just call sqlite3_exec with the proper ATTACH as you would on the command
line. (Of course you could also do a prepare/step/finalize, but for ATTACH
sqlite3_exec is enough.)

Example:

sqlite3 *db = NULL;
/* ... */
sqlite3_exec(db, "ATTACH DATABASE 'filename' AS dbname", NULL, NULL,
);

Using SELECT sqlite_attach('filename','dbname',NULL) should also work -
haven't tried it though. This should have the benefit that the arguments can
be bound and the attach statement can be prepared. I'm not certain this is
possible with the ATTACH syntax. I'll try it soon though.

Mike

-Ursprüngliche Nachricht-
Von: Jon Scully [mailto:[EMAIL PROTECTED]
Gesendet: Donnerstag, 10. Mai 2007 01:18
An: sqlite-users@sqlite.org
Betreff: Re: [sqlite] ATTACH and sqlite3_open()

Simpler than that.  I merely want to attach to two databases (files).
flash.db contains a set of tables that hold non-volatile data; ram.db
contains a set of tables that is re-built (volatile) on re-boot -- but
offers fast, read-only access.  No table-name overlaps, of course.

I want to access both sets of tables, seamlessly, as I should be able to do
using ATTACH, at the command prompt, but do so using the C API.

Just wondering how others do this (Using ATTACH?  Using sqlite3_open()?
Obviously I haven't looked very far into the
sqlite3_open() code to see how it's put together, etc.).

Thanks for the prompt reply.

On 5/9/07, Andrew Finkenstadt <[EMAIL PROTECTED]> wrote:
> On further inspection of your code fragment, it appears you aren't
> really using (extra) attached  databases, but merely specifying an
> alternative file to use if the first file is not available.  Calling
> sqlite3_close(...) will do the right thing, by closing the actual
> database that succeeded in opening.
>
> --andy


-
To unsubscribe, send email to [EMAIL PROTECTED]

-



-
To unsubscribe, send email to [EMAIL PROTECTED]
-




-
To unsubscribe, send email to [EMAIL PROTECTED]
-



AW: [sqlite] ATTACH and sqlite3_open()

2007-05-09 Thread Michael Ruck
Just call sqlite3_exec with the proper ATTACH as you would on the command
line. (Of course you could also do a prepare/step/finalize, but for ATTACH
sqlite3_exec is enough.)

Example:

sqlite3 *db = NULL;
/* ... */
sqlite3_exec(db, "ATTACH DATABASE 'filename' AS dbname", NULL, NULL,
);

Using SELECT sqlite_attach('filename','dbname',NULL) should also work -
haven't tried it though. This should have the benefit that the arguments can
be bound and the attach statement can be prepared. I'm not certain this is
possible with the ATTACH syntax. I'll try it soon though.

Mike

-Ursprüngliche Nachricht-
Von: Jon Scully [mailto:[EMAIL PROTECTED] 
Gesendet: Donnerstag, 10. Mai 2007 01:18
An: sqlite-users@sqlite.org
Betreff: Re: [sqlite] ATTACH and sqlite3_open()

Simpler than that.  I merely want to attach to two databases (files).
flash.db contains a set of tables that hold non-volatile data; ram.db
contains a set of tables that is re-built (volatile) on re-boot -- but
offers fast, read-only access.  No table-name overlaps, of course.

I want to access both sets of tables, seamlessly, as I should be able to do
using ATTACH, at the command prompt, but do so using the C API.

Just wondering how others do this (Using ATTACH?  Using sqlite3_open()?
Obviously I haven't looked very far into the
sqlite3_open() code to see how it's put together, etc.).

Thanks for the prompt reply.

On 5/9/07, Andrew Finkenstadt <[EMAIL PROTECTED]> wrote:
> On further inspection of your code fragment, it appears you aren't 
> really using (extra) attached  databases, but merely specifying an 
> alternative file to use if the first file is not available.  Calling 
> sqlite3_close(...) will do the right thing, by closing the actual 
> database that succeeded in opening.
>
> --andy


-
To unsubscribe, send email to [EMAIL PROTECTED]

-



-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] ATTACH and sqlite3_open()

2007-05-09 Thread Jon Scully

Simpler than that.  I merely want to attach to two databases (files).
flash.db contains a set of tables that hold non-volatile data; ram.db
contains a set of tables that is re-built (volatile) on re-boot -- but
offers fast, read-only access.  No table-name overlaps, of course.

I want to access both sets of tables, seamlessly, as I should be able
to do using ATTACH, at the command prompt, but do so using the C API.

Just wondering how others do this (Using ATTACH?  Using
sqlite3_open()? Obviously I haven't looked very far into the
sqlite3_open() code to see how it's put together, etc.).

Thanks for the prompt reply.

On 5/9/07, Andrew Finkenstadt <[EMAIL PROTECTED]> wrote:

On further inspection of your code fragment, it appears you aren't really
using (extra) attached  databases, but merely specifying an alternative file
to use if the first file is not available.  Calling sqlite3_close(...) will
do the right thing, by closing the actual database that succeeded in
opening.

--andy


-
To unsubscribe, send email to [EMAIL PROTECTED]
-



Re: [sqlite] ATTACH and sqlite3_open()

2007-05-09 Thread Andrew Finkenstadt

On further inspection of your code fragment, it appears you aren't really
using (extra) attached  databases, but merely specifying an alternative file
to use if the first file is not available.  Calling sqlite3_close(...) will
do the right thing, by closing the actual database that succeeded in
opening.

--andy


On 5/9/07, Jon Scully <[EMAIL PROTECTED]> wrote:


Can one attach multiple database files using C API?  The only example
I've seen is CLI-based (using ATTACH).  The Owens book seems to hint
that it's possible to do the same trick using sqlite3_open()...

Quote Pgs. 206-207:
This is more of a connection handle than a database handle since it is
possible to attach multiple databases to a single connection.
However, this connection still represents exactly one transaction
context regardless of how many databases are attached.

But no example is given.  Here's mine:

sqlite3 *db;

if (sqlite3_open("flash.db", )) {
fprintf(stderr, "Can't open the database in the Flash file
system\n");
exit(2);
} else if (sqlite3_open("ram.db", )) {
fprintf(stderr, "Can't open the database in the RAM-disk file
system\n");
sqlite3_close(db);
exit(2);
}

Should this work?  If so, is sqlite3_close() "smart" enough to handle
this situation?  If not, how does sqlite3 (CLI) manage this feat?

Thanks.


-
To unsubscribe, send email to [EMAIL PROTECTED]

-




Re: [sqlite] ATTACH and sqlite3_open()

2007-05-09 Thread Andrew Finkenstadt

From the comments around the attach function, you'd have to execute a SQL

statement "attach database x as y KEY z".  I assume that 'key Z' is for the
encrypting version of SQLite3 distributed by drh.

/*
** An SQL user-function registered to do the work of an ATTACH statement.
The
** three arguments to the function come directly from an attach statement:
**
** ATTACH DATABASE x AS y KEY z
**
** SELECT sqlite_attach(x, y, z)
**
** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
** third argument.
*/

--a


On 5/9/07, Jon Scully <[EMAIL PROTECTED]> wrote:


Can one attach multiple database files using C API?  The only example
I've seen is CLI-based (using ATTACH).  The Owens book seems to hint
that it's possible to do the same trick using sqlite3_open()...

Quote Pgs. 206-207:
This is more of a connection handle than a database handle since it is
possible to attach multiple databases to a single connection.
However, this connection still represents exactly one transaction
context regardless of how many databases are attached.

But no example is given.  Here's mine:

sqlite3 *db;

if (sqlite3_open("flash.db", )) {
fprintf(stderr, "Can't open the database in the Flash file
system\n");
exit(2);
} else if (sqlite3_open("ram.db", )) {
fprintf(stderr, "Can't open the database in the RAM-disk file
system\n");
sqlite3_close(db);
exit(2);
}

Should this work?  If so, is sqlite3_close() "smart" enough to handle
this situation?  If not, how does sqlite3 (CLI) manage this feat?

Thanks.


-
To unsubscribe, send email to [EMAIL PROTECTED]

-




[sqlite] ATTACH and sqlite3_open()

2007-05-09 Thread Jon Scully

Can one attach multiple database files using C API?  The only example
I've seen is CLI-based (using ATTACH).  The Owens book seems to hint
that it's possible to do the same trick using sqlite3_open()...

Quote Pgs. 206-207:
This is more of a connection handle than a database handle since it is
possible to attach multiple databases to a single connection.
However, this connection still represents exactly one transaction
context regardless of how many databases are attached.

But no example is given.  Here's mine:

   sqlite3 *db;

   if (sqlite3_open("flash.db", )) {
   fprintf(stderr, "Can't open the database in the Flash file system\n");
   exit(2);
   } else if (sqlite3_open("ram.db", )) {
   fprintf(stderr, "Can't open the database in the RAM-disk file
system\n");
   sqlite3_close(db);
   exit(2);
   }

Should this work?  If so, is sqlite3_close() "smart" enough to handle
this situation?  If not, how does sqlite3 (CLI) manage this feat?

Thanks.

-
To unsubscribe, send email to [EMAIL PROTECTED]
-