首页 > 解决方案 > 使用 Python (MySQL) 的 Google Cloud Platform SQL:查询返回带有标头的空白数据框

问题描述

我在 GCP 中创建了一个 SQL 数据库,并使用我构建的这个函数通过 Python 将数据上传到它,确保keys参数代表pkeys我的 SQL 表的。

def append_or_update(df,table_name,keys,fields):

    # For this function to work, keys must represent the pkeys of the table
    df = df.reset_index(drop=True)
    cols = keys+fields # arguably fields are just cols ex keys but keeping explicit for now
    df = df[cols]

    # construct list of lists representing values to be passed to query outline
    item_bank = []
    for row in range(len(df)):

        values = [df[i][row] for i in cols]
        values = [int(i) if isinstance(i, (int,np.integer)) else i for i in values]
        values = [float(i) if isinstance(i, (float)) else i for i in values]
        item_bank.append(values)

    # construct query outline
    value_input_string = ','.join(['%s']*df.shape[1])
    header_string = ','.join(cols)
    update_string = ','.join(['{} = VALUES({})'.format(i,i) for i in fields])

    query = '''
    INSERT INTO {} ({}) VALUES({}) ON DUPLICATE KEY UPDATE {}
    '''.format(table_name,header_string,value_input_string,update_string)

    query = query.strip() # remove spaces

    # execute queries using executemany
    cursor.executemany(query, item_bank)

它似乎很好(我检查表,我select * from table limit 3每次迭代都做一次,以确保我仍然可以看到数据。

但是,在此过程中的某个地方,读取开始返回一个空数据帧(带有正确的标头)。无论我写什么查询都会发生这种情况,除了 table_schema 上的查询,这表明表的数据长度很大,同时 GCP 向我显示使用了大量存储,但对实际数据的所有查询都没有返回(具有正确标题的空白数据框)

数据是否在某个时候损坏?这会导致查询返回空白而不是失败吗?到底是怎么回事?

标签: pythonmysqldatabasegoogle-cloud-platform

解决方案


推荐阅读