0

I have a bunch of SQLite db files, and I need to merge them into one big db files.

  • How can I do that?

Added

Based on this, I guess those three commands should merge two db into one.

attach './abc2.db' as toMerge;
insert into test select * from toMerge.test
detach database toMerge

The problem is the db has PRIMARY KEY field, and I got this message - "Error: PRIMARY KEY must be unique".

This is the test table for the db.

CREATE TABLE test (id integer PRIMARY KEY AUTOINCREMENT,value text,goody text)
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • "I need to use Python for building a system to do that." - Any particular reason why it has to be Python? There are some existing merge solutions for SQLite - see http://stackoverflow.com/questions/80801/how-can-i-merge-many-sqlite-databases – Tom Morris Jul 12 '10 at 23:00
  • @Tom : Not much, but Python has the built in SQLite support, and I think it's advantage. Thanks for the hint. – prosseek Jul 13 '10 at 00:52

2 Answers2

3

I'm just thinking off my head here... (and probably after everybody else has moved on, too).

Mapping the primary key to "NULL" should yield the wanted result (no good if you use it as foreign key somewhere else, since the key probably exists, but has different contents)

attach './abc2.db' as toMerge;
insert into test select NULL, value, goody from toMerge.test;
detach database toMerge;

actual test:

sqlite> insert into test select * from toMerge.test;
Error: PRIMARY KEY must be unique
sqlite> insert into test select NULL, value, goody from toMerge.test;
sqlite> detach database toMerge;
1

I'm not 100% sure, but it seems that I should read all the elements and insert the element (except the PRIMARY KEY) one by one into the new data base.

prosseek
  • 182,215
  • 215
  • 566
  • 871