使用 python 读取 oracle sql 文件并将结果提取到 CSV 文件中
read oracle sql file using python and fetch results in to CSV file
我是 python 的初学者,想从 python 读取 sql 文件并将这些结果提取到 CSV 文件中。我试过使用 CX_oracle 连接到 oracle 数据库。当我直接在代码中给出 sql 查询时,它会起作用。但是,我不确定如何从 python.
读取 sql 文件
这是我在代码中直接给出 sql 查询时有效的代码:
import csv
import cx_Oracle
con = cx_Oracle.connect('XCM/XCM@home.com:1500/HOME')
cursor = con.cursor()
csv_file = open("exp.csv", "w")
writer = csv.writer(csv_file, delimiter='|', lineterminator="\n", quoting=csv.QUOTE_NONE)
r = cursor.execute("select * from home_parties where CREATION_DATE >= trunc(sysdate)")
for row in cursor:
writer.writerow(row)
cursor.close()
con.close()
csv_file.close()
我尝试使用以下代码从 sql 文件中获取,该文件具有与上述代码中提到的相同 sql 查询 - sample.sql 但它确实有效
import csv
import cx_Oracle
con = cx_Oracle.connect('XCM/XCM@home.com:1500/HOME')
cursor = con.cursor()
csv_file = open("exp.csv", "w")
writer = csv.writer(csv_file, delimiter='|', lineterminator="\n", quoting=csv.QUOTE_NONE)
f = open('C:/Users/home1/sample.sql')
full_sql = f.read()
r = cursor.execute(full_sql)
for row in cursor:
writer.writerow(row)
cursor.close()
con.close()
csv_file.close()
请帮帮我!!
假设您在同一目录中有一个 sample.sql 文件,第一行是查询。这将从该文件读取 SQL 查询,然后执行查询并将结果保存到名为“exp.csv”的 CSV 文件中。
import cx_Oracle
con = cx_Oracle.connect('XCM/XCM@home.com:1500/HOME')
cursor = con.cursor()
import pandas as pd
with open('sample.sql') as f:
sql_query_string = f.readline();
sql_query = pd.read_sql_query(sql_query_string, con)
sql_query.to_csv("exp.csv", sep='|')
sql 文件的内容可能会在 for 循环 line由行如
import csv
import cx_Oracle
con = cx_Oracle.connect('XCM/XCM@home.com:1500/HOME')
cursor = con.cursor()
sql=""
with open("C:/Users/home1/sample.sql") as f_in:
for line in f_in:
sql += line
f_out = open("exp.csv", "w")
writer = csv.writer(f_out, delimiter='|', lineterminator="\n", quoting=csv.QUOTE_NONE)
cursor.execute(sql)
for row in cursor:
writer.writerow(row)
cursor.close()
con.close()
f_out.close()
f_in.close()
考虑将 SQL 语句分布在多行中
我是 python 的初学者,想从 python 读取 sql 文件并将这些结果提取到 CSV 文件中。我试过使用 CX_oracle 连接到 oracle 数据库。当我直接在代码中给出 sql 查询时,它会起作用。但是,我不确定如何从 python.
读取 sql 文件这是我在代码中直接给出 sql 查询时有效的代码:
import csv
import cx_Oracle
con = cx_Oracle.connect('XCM/XCM@home.com:1500/HOME')
cursor = con.cursor()
csv_file = open("exp.csv", "w")
writer = csv.writer(csv_file, delimiter='|', lineterminator="\n", quoting=csv.QUOTE_NONE)
r = cursor.execute("select * from home_parties where CREATION_DATE >= trunc(sysdate)")
for row in cursor:
writer.writerow(row)
cursor.close()
con.close()
csv_file.close()
我尝试使用以下代码从 sql 文件中获取,该文件具有与上述代码中提到的相同 sql 查询 - sample.sql 但它确实有效
import csv
import cx_Oracle
con = cx_Oracle.connect('XCM/XCM@home.com:1500/HOME')
cursor = con.cursor()
csv_file = open("exp.csv", "w")
writer = csv.writer(csv_file, delimiter='|', lineterminator="\n", quoting=csv.QUOTE_NONE)
f = open('C:/Users/home1/sample.sql')
full_sql = f.read()
r = cursor.execute(full_sql)
for row in cursor:
writer.writerow(row)
cursor.close()
con.close()
csv_file.close()
请帮帮我!!
假设您在同一目录中有一个 sample.sql 文件,第一行是查询。这将从该文件读取 SQL 查询,然后执行查询并将结果保存到名为“exp.csv”的 CSV 文件中。
import cx_Oracle
con = cx_Oracle.connect('XCM/XCM@home.com:1500/HOME')
cursor = con.cursor()
import pandas as pd
with open('sample.sql') as f:
sql_query_string = f.readline();
sql_query = pd.read_sql_query(sql_query_string, con)
sql_query.to_csv("exp.csv", sep='|')
sql 文件的内容可能会在 for 循环 line由行如
import csv
import cx_Oracle
con = cx_Oracle.connect('XCM/XCM@home.com:1500/HOME')
cursor = con.cursor()
sql=""
with open("C:/Users/home1/sample.sql") as f_in:
for line in f_in:
sql += line
f_out = open("exp.csv", "w")
writer = csv.writer(f_out, delimiter='|', lineterminator="\n", quoting=csv.QUOTE_NONE)
cursor.execute(sql)
for row in cursor:
writer.writerow(row)
cursor.close()
con.close()
f_out.close()
f_in.close()
考虑将 SQL 语句分布在多行中