python读取SQLite数据库文件并转csv(.sqlite 文件)
要读取一个SQLite数据库文件(.sqlite 文件),可以使用各种编程语言提供的SQLite库来执行数据库操作。
以下是一个Python的示例,演示如何使用sqlite3库来读取SQLite数据库文件:
import sqlite3
# 连接到SQLite数据库文件
conn = sqlite3.connect('your_database.sqlite')
# 创建一个游标对象
cursor = conn.cursor()
# 执行SQL查询
cursor.execute('SELECT * FROM your_table')
# 获取查询结果
results = cursor.fetchall()
# 处理查询结果
for row in results:
print(row)
# 关闭游标和连接
cursor.close()
conn.close()
请确保Python环境已经安装了sqlite3库:
pip install sqlite3
也可以根据使用的编程语言和库的不同来进行数据库操作,但基本的步骤是连接到数据库,执行SQL查询,获取结果,然后进行处理。
Python代码来获取数据库中的所有表格名称
import sqlite3
# 连接到SQLite数据库文件
conn = sqlite3.connect('your_database.sqlite')
# 创建一个游标对象
cursor = conn.cursor()
# 执行查询以获取所有表格名称
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
# 获取查询结果
tables = cursor.fetchall()
# 打印表格名称
for table in tables:
print(table[0])
# 关闭游标和连接
cursor.close()
conn.close()
将SQLite数据库查询结果保存为CSV文件
import sqlite3
import csv
# 连接到SQLite数据库文件
conn = sqlite3.connect('your_database.sqlite')
# 创建一个游标对象
cursor = conn.cursor()
# 执行SQL查询
cursor.execute('SELECT * FROM your_table')
# 获取查询结果
results = cursor.fetchall()
# 指定要保存的CSV文件名
csv_file = 'output.csv'
# 将查询结果写入CSV文件
with open(csv_file, 'w', encoding='utf-8', newline='') as file:
csv_writer = csv.writer(file)
# 写入CSV文件的标题行(可选)
column_names = [description[0] for description in cursor.description]
csv_writer.writerow(column_names)
# 写入查询结果行
csv_writer.writerows(results)
# 关闭游标和连接
cursor.close()
conn.close()
将SQLite数据库查询结果保存为GeoJSON文件(1)
import sqlite3
import json
# 连接到SQLite数据库文件
conn = sqlite3.connect('../data/port-list.sqlite')
# 创建一个游标对象
cursor = conn.cursor()
# 执行SQL查询获取数据
cursor.execute('SELECT id, country, port, latitude, longitude FROM port_details')
data = cursor.fetchall()
# 构建GeoJSON对象
features = []
for row in data:
feature = {
"type": "Feature",
"properties": {
"id": row[0],
"country": row[1],
"port": row[2]
},
"geometry": {
"type": "Point",
"coordinates": [row[4], row[3]] # 注意经纬度的顺序
}
}
features.append(feature)
geojson_data = {
"type": "FeatureCollection",
"features": features
}
# 将GeoJSON数据写入文件
with open('output.geojson', 'w') as geojson_file:
json.dump(geojson_data, geojson_file)
# 关闭游标和连接
cursor.close()
conn.close()
将SQLite数据库查询结果保存为GeoJSON文件(2)
import sqlite3
import geojson
# 连接到SQLite数据库文件
conn = sqlite3.connect('../data/port-list.sqlite')
cursor = conn.cursor()
# 执行查询
cursor.execute("SELECT id, country, port, latitude, longitude FROM port_details")
# 创建一个FeatureCollection对象
features = []
for row in cursor.fetchall():
id, country, port, latitude, longitude = row
point = geojson.Point((longitude, latitude))
properties = {
"id": id,
"country": country,
"port": port
}
feature = geojson.Feature(geometry=point, properties=properties)
features.append(feature)
feature_collection = geojson.FeatureCollection(features)
# 将FeatureCollection对象转换为GeoJSON字符串
geojson_str = geojson.dumps(feature_collection)
# 关闭游标和连接
cursor.close()
conn.close()
# 将GeoJSON字符串写入文件
with open('port-list.geojson', 'w', encoding='utf-8') as output_file:
output_file.write(geojson_str)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!