python_day11_实践

从数据库读销售额写入text文件

python_day11_实践_第1张图片


from pymysql import Connection

conn = Connection(
    host="localhost",
    port=3306,
    user="root",
    password="root"
)
# 获取游标
cursor = conn.cursor()
conn.select_db("py_sql")

# 执行SQL
cursor.execute("select * from orders")
result = cursor.fetchall()
# print(type(result))

f = open("D:/py_sql.txt", "w", encoding="utf-8")

for element in result:
    # print(element)
    # data_list = list(element)
    my_str = str(element[0]) + ',' + element[1] + ',' + str(element[2]) + ',' + element[3] + '\n'
    f.write(my_str)

# 关闭链接
conn.close()
f.close()

python_day11_实践_第2张图片

你可能感兴趣的:(python,python,数据库,oracle)