Django:将数据从一个数据库复制到另一个数据库 [英] Django: copy data from one database to another

查看:742
本文介绍了Django:将数据从一个数据库复制到另一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个sqlite.db文件。我想将db文件表中的一列内容复制到另一列。



例如:



我在名为new.db的db文件中有模型信息:

  class信息(models.Model):
info_id = models.AutoField(primary_key = True)
info_name = models.CharField(max_length = 50)

和db文件中名为old.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)

我想将列info_id和info_name中的所有数据从old.db复制到new.db中的info_id和info_name中。 / p>

我在想类似的东西:

  manage.py dbshel​​l 

然后

 插入 new.Information( info_id, info_name)
选择 info_id, info_name
从 old 。信息;

这似乎不起作用。它说new。信息表不存在...有什么主意吗?

解决方案

您需要在以下位置切换数据库URL将设置文件复制到db2并运行syncdb来创建新表。之后,执行imo的最简单的操作就是切换回db1并运行。/manage.py dumpdata myapp> data.json ,然后再切换到db2,您可以在其中运行 ./ manage.py loaddata data.json



然后,您可以从db2中删除不需要的数据。



编辑:另一种方法是使用<$ sqlite的c $ c> ATTACH 函数。首先,我建议您执行上述第一步(更改数据库设置并使用syncdb创建表),然后可以切换回去执行以下操作:

  ./ manage.py dbshel​​l 

>附加数据库 new.db作为newdb;
> INSERT INTO newdb.Information SELECT * FROM Information;


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.

for example:

I have the model Information in db file called 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:

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.

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?

解决方案

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.

Afterwards, you can drop the data you don't need from 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:

./manage.py dbshell

> ATTACH DATABASE 'new.db' AS newdb;
> INSERT INTO newdb.Information SELECT * FROM Information;

这篇关于Django:将数据从一个数据库复制到另一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆