0

I want to copy table from another db file but I fail and I can't get why. This is my code:

 $db = new SQLite3($_SERVER['DOCUMENT_ROOT']."/db/098765.db");
 $sql = "ATTACH DATABASE 'admin.db' AS admin ;
         INSERT INTO 'table-1' SELECT * FROM 'admin.table-1';";
 $db->query($sql);

I've read all the questions on this topic on this site, but no answer helped me.

Giving the full path to ATTACH DATABASE doesn't work. Creating table before inserting data also doesn't work.

2
  • Do you absolutely have to do it via php? Commented Sep 7, 2016 at 18:00
  • Preferably, but I'm open to any solution. Commented Sep 7, 2016 at 18:32

3 Answers 3

1

The sqlite3 command line tool has a handy command called .dump that makes this task trivial:

sqlite3 admin.db '.dump "table-1"' | sqlite3 098765.db

This will create the table, all associated indexes and of course it will copy all the data.

Edit: For a more general solution, create a shell script (let's call it copy-table.sh) as follows:

#!/bin/bash
$src_db="$1"
$dst_db="$2"
$table="$3"

sqlite3 "$src_db" ".dump \"$table\"" | sqlite3 "$dst_db"

Then you can execute the script as follows

./copy-table.sh 'admin.db' '098765.db' 'table-1'

Obviously, you can execute the script anyway you want, e.g. from cron or from php.

Sign up to request clarification or add additional context in comments.

5 Comments

I need this script to be executed automatically. Is it possible with command line tools?
Yes. Is it always the same table, i.e. table-1 and the same databases?
No, it differs.
I execute it via shell_exec, but it doesn't do anything. My code $dbPath = $_SERVER['DOCUMENT_ROOT']; echo shell_exec("sh $dbPath/db/copy-table.sh 'admin.db' '098765.db' 'table-1'");
Does it give an error? What about if you execute in the terminal. Note that you have to give the full paths to the databases, e.g. shell_exec("sh $dbPath/db/copy-table.sh '$dbPath/admin.db' '$dbPath/098765.db' 'table-1'");
0
  1. Properly quote database object identifiers (table/column names etc) in your INSERT statement. Use use double quotes instead of single ones, which are for string literals. Better yet don't use dashes or other restricted characters in object names if possible (stick with alphanumerics and underscore).

  2. Use exec() instead of query()

$dbPath = $_SERVER['DOCUMENT_ROOT'];
$adminDbPath = $dbPath; // Or something else

$db = new SQLite3("$dbPath/db/098765.db");
$db->exec("ATTACH DATABASE '$adminDbPath/admin.db' AS admin");
$db->exec('INSERT INTO "table-1" SELECT * FROM admin."table-1"');
     ^^^^              ^       ^                     ^       ^

7 Comments

Warning: SQLite3::exec(): no such table: table-1
You won't get this warning message if you properly double quote the table name "table-1".
I did everything exactly as you wrote.
Apparently not. There are two possibilities: either you didn't properly quoted table names or the table has a different name in one or both databases. Can you post your code after all edits?
I copied yours and double checked it. I also tried CREATE TABLE ... AS, it says that there is no such table : admin.table-1.
|
0

You can get exact copy of table by performing the following set of SQL statements:

(In context of connection to destination database)

attach '<source-db-full-name>' as sourceDb;
select sql from 'sqlite_master' where type = 'table' and name = '<name-of-table>';

// Execute result of previous statement. 
// It will create empty table with 
// schema identical to schema of source table

insert into '<name-of-table>' select * from sourceDb.[<name-of-table>];
detach sourceDb;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.