开发者

Dumping a table's content in sqlite3 to be imported into a new database

开发者 https://www.devze.com 2023-01-13 12:02 出处:网络
Is there an easy way of dumping a S开发者_如何学JAVAQLite database table into a text string with insert statements to be imported into the same table of a different database?

Is there an easy way of dumping a S开发者_如何学JAVAQLite database table into a text string with insert statements to be imported into the same table of a different database?

In my specific example, I have a table called log_entries with various columns. At the end of every day, I'd like to create a string which can then be dumped into an other database with a table of the same structure called archive. (And empty the table log_entries)

I know about the attach command to create new databases. I actually wish to add it to an existing one rather than creating a new one every day.

Thanks!


ATTACH "%backup_file%" AS Backup;
INSERT INTO Backup.Archive SELECT * FROM log_entries;
DELETE FROM log_entries;
DETACH Backup;

All you need to do is replace %backup_file% with the path to your backup database. This approach considers that your Archive table is already defined and that you are using the same database file to cumulate your archive.


$ sqlite3 exclusion.sqlite '.dump exclusion' 
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE exclusion (word string);
INSERT INTO "exclusion" VALUES('books');
INSERT INTO "exclusion" VALUES('rendezvousing');
INSERT INTO "exclusion" VALUES('motherlands');
INSERT INTO "exclusion" VALUES('excerpt');
...
0

精彩评论

暂无评论...
验证码 换一张
取 消