php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27911 ATTACH DATABASE not working
Submitted: 2004-04-07 17:29 UTC Modified: 2004-04-14 06:56 UTC
From: csaba at alum dot mit dot edu Assigned:
Status: Closed Package: SQLite related
PHP Version: 5.0.0RC1 OS: Win 2K
Private report: No CVE-ID: None
 [2004-04-07 17:29 UTC] csaba at alum dot mit dot edu
Description:
------------
I've been using mysqlite regularly for the past two weeks, no complaints.  However, I cannot seem to do a SQLite ATTACH DATABASE at all.  Variations I have tried: sqlite_popen in place of sqlite_open (especially when $db1 was ':memory:', closing $db1 before attaching, using ':memory:' in place of either 'Delme1.db1' or 'Delme2.db1'.  I have also tried this
with and without open_basedir set to "." in my Apache 2.0.48

Csaba Gabor

Reproduce code:
---------------
$db1 = sqlite_open('Delme.db1', 0666, $sqliteerror);
sqlite_query ($db1, 'DROP TABLE Tab1;');
sqlite_query($db1, "CREATE TABLE Tab1 (Id INTEGER PRIMARY KEY, Fld1 VARCHAR(16));"); 
sqlite_query ($db1, "INSERT INTO Tab1 VALUES (NULL, 'foo1');");
print "<br>Rows: in DB1: " . sqlite_fetch_single (sqlite_query($db1, "SELECT COUNT(*) FROM Tab1;"));

$db2 = sqlite_open('Delme.db2', 0666, $sqliteerror);
sqlite_query ($db2, 'DROP TABLE Tab2;');
sqlite_query($db2, "CREATE TABLE Tab2 (Id2 INTEGER PRIMARY KEY, Fld2 VARCHAR(8));"); 
sqlite_query ($db2, "INSERT INTO Tab2 VALUES (NULL, 'bar2');");
sqlite_query ($db2, "INSERT INTO Tab2 VALUES (NULL, 'bar3');");
print "<br>Rows in DB2: " . sqlite_fetch_single (sqlite_query($db2, "SELECT COUNT(*) FROM Tab2;"));

@sqlite_query ($db2, "DETACH DATABASE db;");
sqlite_query ($db2, "ATTACH DATABASE 'Delme.db1' AS db4;");

$aRes = sqlite_array_query ($db2, "SELECT * FROM db4.Tab1;");


Expected result:
----------------
I expect that I can use the attached database as described at SQLite's homepage.

Actual result:
--------------
Instead, in pretty much all the variants that I tried, I get the following error on the ATTACH DATABASE line:

Warning: sqlite_query() [function.sqlite-query]: cannot attach empty database: db4 in MyPHPFilesDir/myPHPCode.php on line 15

The following (last) line also gets a complaint but by then the damage has already been done.

At some point, when I was testing using db3 (in place of where db4 now is), I got the complaint that the database (db3) was already in use.  I don't know how that complaint started originating, nor could I get rid of it.  Going to db4 got me back to the usual complaint I indicated above.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-08 04:37 UTC] wez@php.net
Please try using the full path to the file in your
ATTACH SQL.

PS: it's SQLite, not mysqlite
 [2004-04-08 04:40 UTC] wez@php.net
bah, that's what happens when you get interrupted.
OK, you can't ATTACH a database that you already have
opened elsewhere (like inside PHP); close it first.
 [2004-04-14 06:22 UTC] csaba at alum dot mit dot edu
Thanks for your comments (and sorry about the name typo).  The complete path name has done the trick on my system (and NOT the close database).  I would suppose that what is happening is that in the sqlite_open PHP is looking in the appropriate directory but the ATTACH is passed to sqlite which doesn't know what search path PHP is using.

I tested this by inserting the full path into the ATTACH DATABASE line, doing an insert into the ATTACHED db4.Tab1, and then doing a query on the original $db1 database connection (without a close of $db1).

But IF it is the case that a sqlite_close is not necessary, then using ':memory:' in place of 'Delme.db1' on the first line and on the ATTACH DATABASE line should allow the database in memory to be seen.  However, in the subsequent sqlite_array_query I still get: no such table: db4.Tab1

If it was the case that the database must be closed per your second comments, then the behaviour with ':memory:' is understandable (since it's gone).

Csaba Gabor

PS.  I just wanted to ensure this got reviewed (since it seems to me like the case with ':memory:' is not working if your first comments, per my testing, are correct), so apologies if it was inappropriate to change status to Open (and in that case please let me know what my action should have been).
 [2004-04-14 06:56 UTC] csaba at alum dot mit dot edu
I've been thinking about :memory: as a shared memory object on a per PHP script basis, but it seems that it is non shared memory on a per connection basis.  So the notion of dumping a database into :memory: from one connection and being able to transfer that into the :memory: of another connection is not going to fly.

Csaba
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC