【问题标题】:Using MySQL to create a DB and populate it with SQLAlchemy and Pandas使用 MySQL 创建数据库并使用 SQLAlchemy 和 Pandas 填充它
【发布时间】:2021-06-24 18:01:25
【问题描述】:

我使用 MySQl Work Bench 制作了一个表模式,并希望用我 抓取 的推文填充它。到目前为止,所有外键和主键都已设置。 但我不能让 SQLalchemy 使用模式并填充它。 我用这个命令行试了一下: user_df.to_sql(name='user', con=con, if_exists='append', index=False) 我缩短了表创建的代码以仅匹配“用户”部分。 我得到的错误是 sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1054, "Unknown column 'id' in 'field list'")


SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema CapitolRiot
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema CapitolRiot
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `CapitolRiot` DEFAULT CHARACTER SET utf8 ;
USE `CapitolRiot` ;

-- -----------------------------------------------------
-- Table `CapitolRiot`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CapitolRiot`.`User` (
  `userName` LONGTEXT NULL,
  `displayName` LONGTEXT NULL,
  `description` LONGTEXT NULL,
  `friendsCount` INT NULL,
  `createdAt` DATE NULL,
  `followersCount` INT NULL,
  `statusesCount` INT NULL,
  `favouritesCount` INT NULL,
  `listedCount` INT NULL,
  `mediaCount` INT NULL,
  `location` LONGTEXT NULL,
  `protected` TINYINT(1) NULL,
  `linkUrl` LONGTEXT NULL,
  `linkTcourl` LONGTEXT NULL,
  `profileImageUrl` LONGTEXT NULL,
  `profileBannerUrl` LONGTEXT NULL,
  `url` LONGTEXT NULL,
  `verified` TINYINT(1) NULL,
  `userID` BIGINT NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`userID`))
ENGINE = InnoDB;

CREATE UNIQUE INDEX `userID_UNIQUE` ON `CapitolRiot`.`User` (`userID` ASC) VISIBLE;

【问题讨论】:

  • 张贴user_df.to_sql(name='user', con=con, if_exists='append', index=False) 的错误输出可能会有所帮助,会让外面的人更有可能发现问题。
  • 我编辑了问题以包含您要求的内容
  • 很好,非常有帮助
  • 我试着把它写到表user_df = pandas.json_normalize(user)
  • 找到它...SQLAlchemy 需要我没有在 MySQL 中创建或稍微错误命名的列。修复后,它工作了

标签: mysql pandas sqlalchemy


【解决方案1】:

所以拼凑起来,你就跑了:

user_df = pandas.json_normalize(user)

失败了:

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1054, "Unknown column 'id' in 'field list'")

鉴于您的用户结构看起来像:

{'username': 'EricFlut', 'displayname': 'Eric', 'id': 350130565, 'description': '', 'rawDescription': '', 'descriptionUrls': None, 'verified': False, 'created': '2011-08-07T07:38:54+00:00', 'followersCount': 149, 'friendsCount': 452, 'statusesCount': 756, 'favouritesCount': 141, 'listedCount': 7, 'mediaCount': 31, 'location': '', 'protected': False, 'linkUrl': None, 'linkTcourl': None, 'profileImageUrl': 'https://pbs.twimg.com/profile_images/1347294339672715267/HGJ4s-sv_normal.jpg', 'profileBannerUrl': None, 'url': 'https://twitter.com/EricFlut'}

你的表结构如下:

CREATE TABLE IF NOT EXISTS `CapitolRiot`.`User` (
  `userName` LONGTEXT NULL,
  `displayName` LONGTEXT NULL,
  `description` LONGTEXT NULL,
  `friendsCount` INT NULL,
  `createdAt` DATE NULL,
  `followersCount` INT NULL,
  `statusesCount` INT NULL,
  `favouritesCount` INT NULL,
  `listedCount` INT NULL,
  `mediaCount` INT NULL,
  `location` LONGTEXT NULL,
  `protected` TINYINT(1) NULL,
  `linkUrl` LONGTEXT NULL,
  `linkTcourl` LONGTEXT NULL,
  `profileImageUrl` LONGTEXT NULL,
  `profileBannerUrl` LONGTEXT NULL,
  `url` LONGTEXT NULL,
  `verified` TINYINT(1) NULL,
  `userID` BIGINT NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`userID`))

我认为pymysql.err.OperationalError 最初是被mysql 抛出的,python 只是将它报告给你。我的猜测是 mysql 不知道在哪里写入您提供的 user.id 字段 - 可能是因为 sqlalchemy 没有正确进行映射。

我知道这是一个包含大量内容的紧凑文档,但https://docs.sqlalchemy.org/en/13/orm/tutorial.html#declare-a-mapping 可能是最好的查看位置。

【讨论】:

    猜你喜欢
    • 2019-07-10
    • 1970-01-01
    • 2021-12-18
    • 2012-07-16
    • 2015-08-18
    • 2011-03-04
    • 2018-08-13
    • 1970-01-01
    • 2020-03-05
    相关资源
    最近更新 更多