【发布时间】:2019-04-19 06:13:31
【问题描述】:
我正在创建新表,然后在其中插入值,因为 tsv 文件没有标题,所以我需要先创建表结构然后插入值。我正在尝试将值插入到已创建的数据库表中。我正在使用df.to_sql 函数将tsv 值插入数据库表,但它正在创建表,但它没有在该表中插入值,也没有给出任何类型的错误。
我尝试通过sqalchemy 和insert 值创建新表,但它对已经创建的表不起作用。
conn, cur = create_conn()
engine = create_engine('postgresql://postgres:Shubham@123@localhost:5432/walmart')
create_query = '''create table if not exists new_table(
"item_id" TEXT, "product_id" TEXT, "abstract_product_id" TEXT,
"product_name" TEXT, "product_type" TEXT, "ironbank_category" TEXT,
"primary_shelf" TEXT, apparel_category" TEXT, "brand" TEXT)'''
cur.execute(create_query)
conn.commit()
file_name = 'new_table'
new_file = "C:\\Users\\shubham.shinde\\Desktop\\wallll\\new_file.txt"
data = pd.read_csv(new_file, delimiter="\t", chunksize=500000, error_bad_lines=False, quoting=csv.QUOTE_NONE, dtype="unicode", iterator=True)
with open(file_name + '_bad_rows.txt', 'w') as f1:
sys.stderr = f1
for df in data:
df.to_sql('new_table', engine, if_exists='append')
data.close()
我想将df.to_sql() 中的值插入到数据库表中
【问题讨论】:
-
错误是什么?也请分享一些数据。
-
你能在
df.to_sql之前打印df吗? -
@UpasanaMittal
df正在打印 -
@SupratimHaldar 它没有显示错误问题是它没有将数据加载到表中
标签: python-3.x pandas postgresql sqlalchemy