python - 使用 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 向我显示使用了大量存储,但对实际数据的所有查询都没有返回(具有正确标题的空白数据框)
数据是否在某个时候损坏?这会导致查询返回空白而不是失败吗?到底是怎么回事?
解决方案
推荐阅读
- xpages - 如何将富文本附件从一个文档传输到另一个文档
- netsuite - Suitescript 2.0 Netsuite 中的分页
- javascript - {display:none} 不使用复选框?
- javascript - 单击活动引导选项卡时,滚动顶部位置更改
- git - libgit2:将远程分支引用转换为有效的引用规范
- reactjs - 在 componentDidMount 中触发重新渲染
- vue.js - VueJS:将 v-for 的实例传递给样式时,ReferenceError '未定义'
- javascript - 带函数参数的 Rest 运算符
- javascript - 在 javascript 中将 DateTimeOffset 字符串值转换为 Date 对象
- android - 如何为当前设备设置 WifiP2pDevice.deviceName?