使用python将数据从.csv导入mysql到两个表中

时间:2019-01-29 15:54:42

标签: python mysql csv

表中的数据通过ID具有关联,例如stackoverflow问题具有其标签,作者,发布时间。 尝试编写将标签和作者连接起来以引用并将其插入mysql的代码。 我的报价存储在名为Posts的表中。 标签和作者在表格“条款”中。

Example

1 个答案:

答案 0 :(得分:0)

您的MYSQL模式应使用以下内容创建:

CREATE TABLE Tags (
  `id` smallint NOT NULL AUTO_INCREMENT  ,
  `name` longtext(250) NOT NULL UNIQUE,
 PRIMARY KEY (`id`)
);

CREATE TABLE Authors (
  `id` int AUTO_INCREMENT  ,
  `name` varchar(100) UNIQUE,
 PRIMARY KEY (`id`)
);

CREATE TABLE Posts (
  `id` tinyint unsigned AUTO_INCREMENT  ,
  `author_id` smallint NOT NULL ,
  `tag_id` smallint NOT NULL ,
 PRIMARY KEY (`id`)
);

ALTER TABLE `Posts` ADD FOREIGN KEY (author_id) REFERENCES Authors (`id`);

ALTER TABLE `Posts` ADD FOREIGN KEY (tag_id) REFERENCES Tags (`id`);

Database for storing posts with tag and author associations 您的python代码看起来像这样

import csv
import mysql
# Setup database in some way to connect, depends on how you have your database setup
db

with open('posts.csv', 'rb') as f: #Open the file
    c= csv.reader(f)
    for row in c: #Assume there is no header row and read row by row
        #Get the id of the tag
        db.execute(""" INSERT INTO Tags (`name`) VALUES (%s) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)""", (row[0]))
        tag_id = db.insert_id()

        #Try to insert the author and if it exists get the id
        db.execute(""" INSERT INTO Authors (`name`) VALUES (%s) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)""", (row[1]))
        author_id = db.insert_id()

        #Insert the row into the Posts table
        db.execute(""" INSERT INTO Posts (`tag_id`, `author_id`) VALUES (%s, %s)""", (tag_id, author_id))

这是未经测试的,但应该可以让您对要寻找的东西有所了解。

This might be helpful for the SQL mechanic