首页 > 解决方案 > 使用python将数据从.csv导入mysql到两个表中

问题描述

表中的数据通过 id 建立关系,例如 stackoverflow 问题有其标签、作者、发布时间。试图编写一个代码,将标签和作者连接起来引用并将其插入 mysql。我的报价存储在一个名为 Posts 的表中。标签和作者在一个表中。

例子

标签: pythonmysqlcsv

解决方案


您的 MYSQL Schema 应该使用以下内容创建:

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`);

用于存储带有标签和作者关联的帖子的数据库 你的 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))

这是未经测试的,但应该让您很好地了解要查找的内容。

这可能对 SQL 机制有帮助


推荐阅读