I have two sqlite.db files. I'd like to copy the contents of one column in a table of on db file to another.
我有两个sqlite.db文件。我想将db文件表中一列的内容复制到另一列。
for example:
例如:
I have the model Information in db file called new.db:
我在db文件中有名为new.db的模型信息:
class Information(models.Model):
info_id = models.AutoField(primary_key = True)
info_name = models.CharField( max_length = 50)
and the following information model in db file called old.db:
以及名为old.db的db文件中的以下信息模型:
class Information(models.Model):
info_id = models.AutoField(primary_key = True)
info_type = models.CharField(max_length = 50)
info_name = models.CharField( max_length = 50)
I'd like to copy all the data in column info_id and info_name from old.db to info_id and info_name in new.db.
我想将old.db中的info_id和info_name列中的所有数据复制到new.db中的info_id和info_name。
I was thinking something like:
我想的是:
manage.py dbshell
then
然后
INSERT INTO "new.Information" ("info_id", "info_name")
SELECT "info_id", "info_name"
FROM "old.Information";
This doesn't seem to be working. It says new.Information table does not exist... any ideas?
这似乎不起作用。它说new.Information表不存在......任何想法?
2 个解决方案
#1
5
You'd need to switch your database URL in your settings file to db2 and run syncdb to create the new tables. After that the easiest thing to do imo would be to switch back to db1 and run ./manage.py dumpdata myapp > data.json
, followed by another switch to db2 where you can run ./manage.py loaddata data.json
.
您需要将设置文件中的数据库URL切换到db2并运行syncdb以创建新表。之后,最简单的做法就是切换回db1并运行./manage.py dumpdata myapp> data.json,然后再转到db2,在那里你可以运行./manage.py loaddata data.json。
Afterwards, you can drop the data you don't need from db2.
之后,您可以从db2中删除不需要的数据。
Edit: Another approach would be to use the ATTACH
function from sqlite. First, I recommend you do the first step above (change database settings and use syncdb to create the tables), then you can switch back and do this:
编辑:另一种方法是使用sqlite的ATTACH函数。首先,我建议您执行上面的第一步(更改数据库设置并使用syncdb创建表),然后您可以切换回来执行此操作:
./manage.py dbshell
> ATTACH DATABASE 'new.db' AS newdb;
> INSERT INTO newdb.Information SELECT * FROM Information;
#2
1
- The dumped file from old.db contains info_type field which is not in the new Information model. This will fail the loaddata which checks all field loaded from JSON file. You could comment out info_type line before dump from old model.
- old.db中的转储文件包含info_type字段,该字段不在新的信息模型中。这将使loaddata失败,该loaddata检查从JSON文件加载的所有字段。您可以在从旧模型转储之前注释掉info_type行。
-
The Attach way mentioned by Alex is easier and great, which needs a tiny tweak
Alex提到的Attach方式更简单,更好,需要微调
INSERT INTO newdb.Information SELECT * FROM Information;
INSERT INTO newdb.Information SELECT * FROM Information;
note the missing parentheses around the SELECT, sqlite does not accept them. Refs http://sqlite.org/lang_insert.html
请注意SELECT周围缺少括号,sqlite不接受它们。参考http://sqlite.org/lang_insert.html
- If you are performing migration, have you tried South
- 如果您正在进行迁移,请尝试南方
#1
5
You'd need to switch your database URL in your settings file to db2 and run syncdb to create the new tables. After that the easiest thing to do imo would be to switch back to db1 and run ./manage.py dumpdata myapp > data.json
, followed by another switch to db2 where you can run ./manage.py loaddata data.json
.
您需要将设置文件中的数据库URL切换到db2并运行syncdb以创建新表。之后,最简单的做法就是切换回db1并运行./manage.py dumpdata myapp> data.json,然后再转到db2,在那里你可以运行./manage.py loaddata data.json。
Afterwards, you can drop the data you don't need from db2.
之后,您可以从db2中删除不需要的数据。
Edit: Another approach would be to use the ATTACH
function from sqlite. First, I recommend you do the first step above (change database settings and use syncdb to create the tables), then you can switch back and do this:
编辑:另一种方法是使用sqlite的ATTACH函数。首先,我建议您执行上面的第一步(更改数据库设置并使用syncdb创建表),然后您可以切换回来执行此操作:
./manage.py dbshell
> ATTACH DATABASE 'new.db' AS newdb;
> INSERT INTO newdb.Information SELECT * FROM Information;
#2
1
- The dumped file from old.db contains info_type field which is not in the new Information model. This will fail the loaddata which checks all field loaded from JSON file. You could comment out info_type line before dump from old model.
- old.db中的转储文件包含info_type字段,该字段不在新的信息模型中。这将使loaddata失败,该loaddata检查从JSON文件加载的所有字段。您可以在从旧模型转储之前注释掉info_type行。
-
The Attach way mentioned by Alex is easier and great, which needs a tiny tweak
Alex提到的Attach方式更简单,更好,需要微调
INSERT INTO newdb.Information SELECT * FROM Information;
INSERT INTO newdb.Information SELECT * FROM Information;
note the missing parentheses around the SELECT, sqlite does not accept them. Refs http://sqlite.org/lang_insert.html
请注意SELECT周围缺少括号,sqlite不接受它们。参考http://sqlite.org/lang_insert.html
- If you are performing migration, have you tried South
- 如果您正在进行迁移,请尝试南方