简体   繁体   English

如何将数据转储 (SqLite3) 中的数据导入 django?

[英]How to import the data from a data dump (SqLite3) into django?

I have a data dump file(.sql) containing certain data, I would like to import it to my database(currently Sqlite3 but thinking about changing to MySQL) in order to use it in my test website.我有一个包含某些数据的数据转储文件(.sql),我想将其导入我的数据库(目前是 Sqlite3,但考虑更改为 MySQL),以便在我的测试网站中使用它。 Also, I need to know if it's possible to add the models too automatically, I presume it needs to be added manually, but any how, if there is any way to solve it, please suggest it.另外,我需要知道是否可以自动添加模型,我认为它需要手动添加,但是无论如何,如果有任何方法可以解决它,请提出建议。

There is a way to help you generate the Django models automatically given you have an existing database.如果您有一个现有的数据库,有一种方法可以帮助您自动生成 Django 模型。 However this is a shortcut.然而,这是一条捷径。 You may then fine tune your models and add them to your app as needed.然后,您可以微调您的模型并根据需要将它们添加到您的应用程序中。

Structuring your models in apps might force you to use the Models db_table meta option.在应用程序中构建模型可能会迫使您使用 Models db_table元选项。

If at some point you would like to switch databases (Sqlite3 -> MySQL) you can export (dump) your current data to json.如果您想切换数据库(Sqlite3 -> MySQL),您可以将当前数据导出(转储)到 json。 Then you could import (load) the data to the new database (after creating the database tables withmigrate command).然后您可以将数据导入(加载)到新数据库(在使用migrate命令创建数据库表之后)。 To do this you can use Django management commands:为此,您可以使用 Django 管理命令:

I was able to get an alternative answer after researching a bit.经过一番研究,我能够得到另一个答案。 Since I'm having a PostgreSQL data dump file with a file extension '.sql', I was capable of running a single command that imported the whole data dump into my local database, which is PostgreSQL.由于我有一个带有文件扩展名“.sql”的 PostgreSQL 数据转储文件,因此我能够运行将整个数据转储导入本地数据库的单个命令,即 PostgreSQL。 I'm using PgAdmin4 as my database management system and I installed psql during the installation of PgAdmin4, I added the psql to the path of my command prompt, hence it was accessible.我使用 PgAdmin4 作为我的数据库管理系统,并在安装 PgAdmin4 期间安装了 psql,我将 psql 添加到了命令提示符的路径中,因此它可以访问。 In order to import the data dump, I used the command provided below,为了导入数据转储,我使用了下面提供的命令,

psql -U <username> -d <database_name> < <file.sql>

The '<' after database_name is necessary, so be sure to include it. database_name 后面的 '<' 是必需的,所以一定要包含它。 Here is the username of the configured account, is the database to which the data dump should be added, , is the file containing the data dump.这里是配置帐户的用户名,是应该添加数据转储的数据库,是包含数据转储的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM