我想将csv文件导入postgres数据库,但是数据库仅显示列名。我的问题是如何将值导入到我创建的表中? 这是我的代码:
@GetMapping(value = "/get", consumes = MediaType.ALL_VALUE)
以下结果显示了值,但在postgres数据库中未显示。我尝试了import psycopg2
import csv
conn = psycopg2.connect(host="127.0.0.1", port="5432", database="postgres", user="postgres", password="******")
print "Opened database successfully"
cur = conn.cursor()
cur.execute('''create table calls_aapl("Ask" int,"Bid" int,"Change" int,"ContractSymbol" varchar(50),"ImpliedVolatility" decimal(5,4),"LastPrice" money,
"LastTradeDate" date,"OpenInterest" int,"PercentChange" decimal(5,4),"Strike" int,"Volume" int);''')
print "Table created successfully"
reader = csv.reader(open('D:/python/Anaconda/AAPL_Data/Calls.csv', 'r'))
for i, row in enumerate(reader):
print(i, row)
if i >= 0: continue
cur.execute('''
INSERT INTO calls_aapl(
"Ask", "Bid", "Change", "ContractSymbol", "ImpliedVolatility", "LastPrice", "LastTradeDate", "OpenInterest", "PercentChange", "Strike", "Volume"
) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s), [tuple(row)]'''
)
conn.commit()
cur.close()
和copy_from()
仍然不起作用。
copy_expert()