#-*- coding:utf-8-*
import sqlite3
import csv
database_path="D:\\desktop\\ni.db"
# CSV 文件名
csv_file = 'D:\\desktop\\output.csv'
# 创建与数据库的连接
conn = sqlite3.connect(database_path) # 替换为你的数据库文件名
# 创建一个游标对象
cursor = conn.cursor()
# 执行查询语句
cursor.execute("SELECT * FROM 日常测试2")
# 获取所有行
rows = cursor.fetchall()
print(rows[1])
# 获取列名
column_names = [description[0] for description in cursor.description]
# 写入 CSV 文件
with open(csv_file, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(column_names) # 写入列名
writer.writerows(rows) # 写入数据行
# 关闭游标
cursor.close()
# 关闭数据库连接
conn.close()
print(f"Data has been exported to {csv_file}")
04-28
1921
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-29
3658
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-08
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-15
1120
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-16
8342
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)