【发布时间】:2020-08-06 12:51:52
【问题描述】:
我正在尝试使用 psycopg2 将我在代码中创建的一些表存储在 RDS 实例中。该脚本运行没有问题,我可以看到该表已正确存储在数据库中。但是,如果我尝试检索查询,我只会看到列,但看不到数据:
import pandas as pd
import psycopg2
test=pd.DataFrame({'A':[1,1],'B':[2,2]})
#connect is a function to connect to the RDS instance
connection= connect()
cursor=connection.cursor()
query='CREATE TABLE test (A varchar NOT NULL,B varchar NOT NULL);'
cursor.execute(query)
connection.commit()
cursor.close()
connection.close()
此脚本运行没有问题,并从以下脚本打印出file_check:
connection=connect()
# check if file already exists in SQL
sql = """
SELECT "table_name","column_name", "data_type", "table_schema"
FROM INFORMATION_SCHEMA.COLUMNS
WHERE "table_schema" = 'public'
ORDER BY table_name
"""
file_check=pd.read_sql(sql, con=connection)
connection.close()
我明白了:
table_name column_name data_type table_schema
0 test a character varying public
1 test b character varying public
看起来不错。
但是运行以下命令:
read='select * from public.test'
df=pd.read_sql(read,con=connection)
返回:
Empty DataFrame
Columns: [a, b]
Index: []
有人知道为什么会这样吗?我似乎无法解决这个问题
【问题讨论】:
标签: python sql pandas amazon-rds psycopg2