我有一个想与Python一起运行的Postgresql SQL查询,并将其导出到CSV文件。
我对Python还是很陌生,但是我设法编写了一个可以运行查询并导出到文件的脚本。
import psycopg2
# File path and name.
fileName = 'test.csv'
# Database connection variable.
connect = None
# Check if the file path exists.
if os.path.exists(filePath):
try:
# Connect to database.
connect = psycopg2.connect(host="xxxx", port="5439", database="xxxx", user="xxxx", password="xxxx")
except psycopg2.DatabaseError as e:
# Confirm unsuccessful connection and stop program execution.
print("Database connection unsuccessful.")
quit()
# Cursor to execute query.
cursor = connect.cursor()
# SQL to select data from the person table.
sqlSelect = """
SELECT * FROM TABLE
"""
try:
# Execute query.
cursor.execute(sqlSelect)
# Fetch the data returned.
results = cursor.fetchall()
# Extract the table headers.
headers = [i[0] for i in cursor.description]
#Print the results
#print(pd.read_sql(sqlSelect, connect))
print(tb.tabulate(results, headers=headers, tablefmt='psql', showindex="always", floatfmt=".10f"))
# Open CSV file for writing.
csvFile = csv.writer(open(filePath + fileName, 'w', newline=''),
delimiter=',', lineterminator='\r\n',
quoting=csv.QUOTE_ALL, escapechar='\\')
# Add the headers and data to the CSV file.
csvFile.writerow(headers)
for row in results:
csvFile.writerow(row)
# Message stating export successful.
print("Data export successful.")
# csvFile.close()
except psycopg2.DatabaseError as e:
# Message stating export unsuccessful.
print("Data export unsuccessful.")
quit()
finally:
# Close database connection.
cursor.close()
connect.close()
else:
# Message stating file path does not exist.
print("File path does not exist.")
cursor.close()
connect.close()
我运行的查询生成70行结果(当我通过数据库程序运行它时)。但是,当我将数据导出到CSV时,它仅导出48行。
我不知道怎么了。
答案 0 :(得分:0)
您的代码对我有用,在postgres中有490行。
也许您的问题出在具有某些特殊字符的记录中,这些记录具有某些特殊字符,导致代码停止并且无法获得预期的结果。
调试脚本,添加异常打印以检查是否是问题所在。