使用Python读取SQLite文件

2024-04-18 11:54:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个作业,在那里我们得到了一堆csv文件来处理和提取信息。我已经成功地完成了那部分。作为一个额外的问题,我们有一个扩展名为.db的SQlite文件。我想知道是否存在将这些文件转换为.csv或直接读取它们的模块?在

如果这样的方法不存在,我可能会将文件插入数据库,并使用python sqlite3模块提取我需要的数据。在


Tags: 模块文件csv数据方法信息数据库db
1条回答
网友
1楼 · 发布于 2024-04-18 11:54:13

您可以使用sqlite commandline tool将表数据转储到CSV。在

To export an SQLite table (or part of a table) as CSV, simply set the "mode" to "csv" and then run a query to extract the desired rows of the table.

sqlite> .header on
sqlite> .mode csv
sqlite> .once c:/work/dataout.csv
sqlite> SELECT * FROM tab1;

In the example above, the ".header on" line causes column labels to be printed as the first row of output. This means that the first row of the resulting CSV file will contain column labels. If column labels are not desired, set ".header off" instead. (The ".header off" setting is the default and can be omitted if the headers have not been previously turned on.)

The line ".once FILENAME" causes all query output to go into the named file instead of being printed on the console. In the example above, that line causes the CSV content to be written into a file named "C:/work/dataout.csv".

http://www.sqlite.org/cli.html

相关问题 更多 >