UBan
UBan

Reputation: 29

backup of sqlite3 database using tcl

I have a database global.sqlite3 and I am trying to create its backup from tcl. But the backup command doesnot work. It gives an error when I run the tcl file. Also the new copy forms that is bkupglobal.sqlite3 get formed but when I open it its empty.

The error is backup failed: unknown database "/home/urmita.banerjee/global.sqlite3.

The code is following :

package require sqlite3

sqlite3 db global.sqlite3

db backup  "/home/urmita.banerjee/global.sqlite3" "/home/urmita.banerjee/bkupglobal.sqlite3"

Could anyone please help with this.

Upvotes: 0

Views: 180

Answers (1)

glenn jackman
glenn jackman

Reputation: 246807

In this context, "database" refers to a schema name within file.

Let's create a db in Tcl:

$ tclsh
% package req sqlite3
3.11.0
% sqlite3 db test.db
% db eval {CREATE TABLE t1(a int, b text)}
% db close
% exit

Now, let's see what we have using the sqlite3 command line tool

$ sqlite3 test.db
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> .schema
CREATE TABLE t1(a int, b text);
sqlite> .databases
seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main             /home/jackman/tmp/test.db                                 
1    temp                                                                       
sqlite> .q

We can see that the default database name in that file is "main". Let's add a row and make a backup:

$ tclsh
% package req sqlite3
3.11.0
% sqlite3 db test.db
% db eval {insert into t1 values (42,"foo")}
% db backup main test_bak.db
% exit

$ ls -l test*.db
-rw-r--r-- 1 jackman jackman 2048 Jun 28 14:41 test.db
-rw-r--r-- 1 jackman jackman 2048 Jun 28 14:43 test_bak.db

$ sqlite3 test.db
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> .schema
CREATE TABLE t1(a int, b text);
sqlite> select * from t1;
42|foo
sqlite> .q

$ sqlite3 test_bak.db
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> .schema
CREATE TABLE t1(a int, b text);
sqlite> select * from t1;
42|foo
sqlite> .q

Upvotes: 1

Related Questions