简体   繁体   English

从.csv文件将数据插入多个MySQL数据库表

[英]Insert Data to multiple MySQL database tables from .csv file

I have a database with tables: person, player, coach, and team. 我有一个包含表格的数据库:人员,玩家,教练和团队。 All the tables have an auto-increment id field as the primary key. 所有表都有一个自动增量id字段作为主键。 Person has id, firstname, lastname. 人有id,firstname,lastname。 Player and coach both have the id field, as well as person_id and team_id as foreign keys to tie them to a team.id or person.id field in the other tables. 玩家和教练都有id字段,以及person_id和team_id作为外键,将它们绑定到其他表中的team.id或person.id字段。

Now in order to fully populate these tables, I have several csv files with the list of names of the players in each team. 现在为了完全填充这些表,我有几个csv文件,其中包含每个团队中玩家的名字列表。 Can I write a bash or python script to take this data and input not only the names to the person table, but also have the proper person and team id values put into the player table? 我可以编写一个bash或python脚本来获取这些数据,不仅可以输入人员表中的名称,还可以将适当的人员和团队ID值放入播放器表中吗?

If the question isn't clear just ask and I'll do what I can to clarify. 如果问题不明确,请问我,我会尽我所能澄清。 Thanks. 谢谢。

mysql> describe person;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(30) | NO   |     | NULL    |                |
| lastname  | varchar(30) | NO   |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
mysql> describe player;
+-----------+---------+------+-----+---------+----------------+
| Field     | Type    | Null | Key | Default | Extra          |
+-----------+---------+------+-----+---------+----------------+
| id        | int(11) | NO   | PRI | NULL    | auto_increment |
| person_id | int(11) | NO   | MUL | NULL    |                |
| team_id   | int(11) | NO   | MUL | NULL    |                |
+-----------+---------+------+-----+---------+----------------+
mysql> describe team;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| teamname  | varchar(25) | NO   |     | NULL    |                |
| location  | varchar(40) | NO   |     | NULL    |                |
| city      | varchar(25) | NO   |     | NULL    |                |
| state     | varchar(2)  | NO   |     | NULL    |                |
| venue     | varchar(35) | NO   |     | NULL    |                |
| league_id | int(11)     | NO   | MUL | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+

And here is an example of the csv file content: (AL-Central-Indians.csv) 以下是csv文件内容的示例:(AL-Central-Indians.csv)

Fausto,Carmona
Carlos,Carrasco
Kelvin,De La Cruz
Chad,Durbin

Using an ORM might be an overkill for your purpose, but it makes your life easy if you need to do real work with data. 使用ORM可能对您的目的而言过于苛刻,但如果您需要对数据进行实际操作,它会让您的生活变得轻松。 It will require you to install some software, but if you are willing to learn some new stuff, you will probably gain a lot down the road. 它需要你安装一些软件,但如果你愿意学习一些新的东西,你可能会获得很多东西。 Luckily, it is not very hard to start using it, for example, using Django: 幸运的是,开始使用它并不是很难,例如,使用Django:

  • Download and install django 下载并安装django
  • Create a new project using django-admin startproject myproject 使用django-admin startproject myproject创建一个新项目
  • Create a new app: ./manage.py startapp myapp 创建一个新的应用程序:./ manage.py startapp myapp
  • Change the database connection parameters in settings.py 更改settings.py中的数据库连接参数
  • ./manage.py inspectdb should create the models for you. ./manage.py inspectdb应该为你创建模型。 Use ./manage.py inspectdb > myapp/models.py to save it. 使用./manage.py inspectdb > myapp/models.py进行保存。
  • Execute export DJANGO_SETTINGS_MODULE=settings to allow you to use django from command line scripts 执行export DJANGO_SETTINGS_MODULE=settings以允许您从命令行脚本使用django

Now you can create an import_players.py script in this fashion: 现在,您可以以这种方式创建import_players.py脚本:

from myapp.models import Player, Person, Coach, Team
for my_file in my_files: # TODO: Iterate through your files
    team = Team.objects.create(name=my_team_name) # creates a db record for a team
    for line in lines_in_my_file: # TODO: Iterate through lines in your file
        player = Player.objects.create(name=my_player_name, team=team) creates a db record for a player

See this to learn how to work with models: https://docs.djangoproject.com/en/dev/topics/db/models/ 了解如何使用模型: https//docs.djangoproject.com/en/dev/topics/db/models/

You can do this directly using the mysql command as follows: 您可以使用mysql命令直接执行此操作,如下所示:

load data local infile 'AL-Central-Indians.csv' into table player
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(person_id, team_id)

I got that from here . 我从这里得到了。 Although that page also deals with exporting excel into CSV first. 虽然该页面还涉及首先将Excel导出为CSV。

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

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