【问题标题】:Cannot populate MySQL database from pandas dataframe with if_exists='append'无法使用 if_exists='append' 从 pandas 数据框填充 MySQL 数据库
【发布时间】:2019-06-06 16:03:22
【问题描述】:

我正在尝试编写一个脚本来使用多个 pandas 数据框填充 mySQL 数据库。为了简单起见,我将在这里演示使用单个 pandas df 填充数据库的代码

我连接到数据库如下:

导入 mysql.connector 将熊猫导入为 pd

# create the cursor and the connector
conn = mysql.connector.connect(
        host='localhost',
        user='root',
        password='my_password')

c = conn.cursor(buffered=True)

# Create the database
c.execute('CREATE DATABASE IF NOT EXISTS ss_json_interop')


# Connect now to the ss_json_interop database
conn = mysql.connector.connect(
            host='localhost',
            user='root',
            password='my_password', 
            database='ss_json_interop')

c = conn.cursor(buffered=True)



#### Create the table
c.execute("""CREATE TABLE IF NOT EXISTS sample_sheet_stats_json (
        ss_ID int NOT NULL AUTO_INCREMENT,
        panel text,
        run_ID text,
        sample_ID text,
        i7_index_ID text,
        i7_index_seq text,
        i5_index_ID text,
        i5_index_seq text,
        number_reads_lane1 varchar(255),
        number_reads_lane2 varchar(255),
        total_reads varchar(255),
        PRIMARY KEY (ss_ID)
        )""")


#### create the engine
# more here: https://stackoverflow.com/questions/16476413/how-to-insert-pandas-dataframe-via-mysqldb-into-database
database_username = 'root'
database_password = 'my_password'
database_ip       = '127.0.0.1'
database_name     = 'ss_json_interop'
database_connection = sqlalchemy.create_engine('mysql+mysqlconnector://{0}:{1}@{2}/{3}'.
                                               format(database_username, database_password, 
                                                      database_ip, database_name))

# define the engine
engine = create_engine("mysql+mysqldb://root:my_password@localhost/sample_sheet_stats_json")

我正在尝试将我的df 填充到一个名为sample_sheet_stats_json 的表中。如果我这样做:

df.to_sql('sample_sheet_stats_json', con=database_connection, if_exists='replace')

该命令有效,并且数据库中的表已正确填充。但是,如果我将if_exists='replace' 替换为if_exists='append'

df.to_sql('sample_sheet_stats_json', con=database_connection, if_exists='append')

我收到一条很长的错误消息,如下所示:(错误消息不完整。它继续复制我的 df 的结构

(mysql.connector.errors.ProgrammingError) 1054 (42S22): Unknown column 'index' in 'field list' [SQL: 'INSERT INTO sample_sheet_stats_json 

很奇怪,我可以执行df.to_sql('sample_sheet_stats_json', con=database_connection, if_exists='append'),只要我运行df.to_sql('sample_sheet_stats_json', con=database_connection, if_exists='replace before'),即如果表格已经填充。

here 已经报告了同样的问题。但是,如果我这样做:

df.to_sql('sample_sheet_stats_json', engine, if_exists='append')

我收到以下错误消息:

(_mysql_exceptions.OperationalError) (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)") (Background on this error at: http://sqlalche.me/e/e3q8)

这没有多大意义,因为我已经可以使用其他命令连接到数据库,如上所示。

有人知道我该如何解决吗?

【问题讨论】:

    标签: mysql python-3.x pandas


    【解决方案1】:

    我已经弄清楚发生了什么。错误信息是在 pandas 数据框中没有列索引,这实际上是正确的。

    因此我必须简单地使用命令df.to_sql('sample_sheet_stats_json', con=database_connection, if_exists='append') 传递参数index=False

    df.to_sql('sample_sheet_stats_json', con=database_connection, if_exists='append', index=False)
    

    这样就解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 2014-03-15
      • 2018-08-13
      • 2016-07-07
      • 2012-07-22
      • 2021-07-17
      • 2022-06-13
      相关资源
      最近更新 更多