Search code examples
sqldjangosqlitedjango-modelsrawsql

Django copying sqlite table columns


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 1:

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 2:

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 db file 1 to info_id and info_name in db file 2.

I understand Raw SQL would be needed.. An example would be appreciated. Thoughts?


Solution

  • If you are trying to do this because you changed your "Information" Model and you would now like to update your database to include the extra field then please have a look at Django's documentation (http://www.djangobook.com/en/1.0/chapter05/) at the section entitled "Making Changes To A Database Schema" for detailed information.

    If you simply want to copy data between two tables, you are going to have to bring both of them to the same schema (with different names of course) and then run an SQL query like "INSERT INTO new_Information(info_id,info_name) select info_id,info_name from old_Information" from the sqlite admin program.