• sqlite的csv的导入,导出


    -----------------------------------以下这段,是十分容易百度到的,但它不是csv的导入导出的适用方法,更适合格式化输出,说白了就是方便人看,它会给长文本的字段数据添加空格和适当截断。这些对于需要后续程序处理的导出操作是致命的。

    导入
    命令: .import
    sqlite> .import 文件名 表名
    注1: 不要忘了开头的点
    注2: 这条语句不能用分号结束. 非SQL不需要分号结束.
    注3: 需要查看默认的分隔符separator. 必须一致. 如果不一致可能导致sqlite字段分割错误.
    查看分隔符使用命令 .show , 如果不一致可直接修改, 比如:
    sqlite>.separator ","
    将分隔符转为逗号.
    举例1:
    将文件a.txt中的数据导入表 tab_xx. (a.csv中字段以逗号分割)
    sqlite> .separator ","
    sqlite> .import a.txt tab_xx
    sqlite>
    导入结束.

    导出
    实现方式: 将输出重定向至文件.
    命令: .output
    sqlite> .output a.txt
    然后输入sql语句, 查询出要导的数据. 查询后,数据不会显示在屏幕上,而直接写入文件.
    结束后,输入
    sqlite> .output stdout
    将输出重定向至屏幕.
    举例2:
    将 tab_xx 中的数据导出到文件a.txt
    sqlite> .output a.txt
    sqlite> select * from tab_xx;
    sqlite> .output stdout
    导出完毕.

    如需导成csv格式,直接将文件a.txt换成a.csv即可

    -----------------------------------根据官网的documents 应该用以下方法

    CSV Import

    Use the ".import" command to import CSV (comma separated value) data into an SQLite table. The ".import" command takes two arguments which are the name of the disk file from which CSV data is to be read and the name of the SQLite table into which the CSV data is to be inserted.

    Note that it is important to set the "mode" to "csv" before running the ".import" command. This is necessary to prevent the command-line shell from trying to interpret the input file text as some other format.

    sqlite> .mode csv
    sqlite> .import C:/work/somedata.csv tab1

    There are two cases to consider: (1) Table "tab1" does not previously exist and (2) table "tab1" does already exist.

    In the first case, when the table does not previously exist, the table is automatically created and the content of the first row of the input CSV file is used to determine the name of all the columns in the table. In other words, if the table does not previously exist, the first row of the CSV file is interpreted to be column names and the actual data starts on the second row of the CSV file.

    For the second case, when the table already exists, every row of the CSV file, including the first row, is assumed to be actual content. If the CSV file contains an initial row of column labels, that row will be read as data and inserted into the table. To avoid this, make sure that table does not previously exist.

    CSV Export

    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;
    sqlite> .system c:/work/dataout.csv

    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".

    The final line of the example (the ".system c:/work/dataout.csv") has the same effect as double-clicking on the c:/work/dataout.csv file in windows. This will typically bring up a spreadsheet program to display the CSV file. That command only works as shown on Windows. The equivalent line on a Mac would be ".system open /work/dataout.csv". On Linux and other unix systems you will need to enter something like ".system libreoffice /work/dataout.csv", substituting your preferred CSV viewing program for "libreoffice".

  • 相关阅读:
    Spring学习(21)--- AOP之Advice应用(上)
    Spring学习(20)--- Schema-based AOP(基于配置的AOP实现) -- 配置切入点pointcut
    Spring学习(19)--- Schema-based AOP(基于配置的AOP实现) --- 配置切面aspect
    Spring学习(18)--- AOP基本概念及特点
    Spring学习(17)--- 三种装配Bean方式比较
    Spring学习(16)--- 基于Java类的配置Bean 之 基于泛型的自动装配(spring4新增)
    Spring学习(15)--- 基于Java类的配置Bean 之 @Bean & @Scope 注解
    怎么找到与你Eclipse匹配的spring tool suite插件
    eclipse安装hibernate插件(在线Marketplace中安装)
    从配置maven环境到maven项目的新建
  • 原文地址:https://www.cnblogs.com/lightwind/p/4550436.html
Copyright © 2020-2023  润新知