简体   繁体   中英

In Linux, is there a command for converting a CSV file into an SQLite file?

Is there a Linux command that will convert a CSV file into an SQLite database, sometime like:

csv2sqlite input.csv output.sqlite [optional_table_schema]

Or does sqlite3 have command-line options that will do this?

(I wrote some code to process/clean some text files. At the end, I may call convert a CSV file into an SQLite database.)

If your csv is very very simple (no quoting, escaping, etc) you can import it with the sqlite shell:

http://www.sqlite.org/cvstrac/wiki?p=ImportingFiles

Edit: But it probably isn't, so I think you'll need to convert the file first. If your csv file no ascii double quotes, or newlines, you can convert it into a file that sqlite can import with this command:

csvtool -u '`' cat csv.csv

(replacing the ` above with some character that's not in your data)

That removes quotes from around fields that have double quotes in them. csvtool doesn't seem to be smart about escaping. It doesn't have any options I can see about handling escaping, and any fields that have " in them, it just leaves as is with the quotes around.

anyway, then you can use the link above for how to import to sqlite. Just make sure you set the same separator

Consider a CSV file my_input.csv with the following data. Note that the pipe ("|") is the default delimiter in SQLite.

apple|red
banana|yellow
celery|green

Below are two methods of creating an SQLite database my_db.sqlite with the above input file.

Method 1: Paste the following code onto the command line. Or paste it into an executable file and run the file in bash.

sqlite3 my_db.sqlite "CREATE TABLE my_table(food TEXT, color TEXT)"
sqlite3 my_db.sqlite ".import my_input.csv my_table"

Method 2: Paste the following Python code into a file and run it using Python 2.6.6, 2.7.1, or 3.1.3.

import subprocess

args = [
    "sqlite3",
    "my_db.sqlite",
    "CREATE TABLE my_table(food TEXT, color TEXT)",
    ]

subprocess.call(args)

args = [
    "sqlite3",
    "my_db.sqlite",
    ".import my_input.csv my_table",
    ]

subprocess.call(args)

Check out termsql. https://gitorious.org/termsql https://gitorious.org/termsql/pages/Home

It converts text to SQL on the command line. (CSV is just text)

Example:

cat textfile | termsql -o sqlite.db

By default the delimiter is whitespace, so to make it work with CSV that is using commata, you'd do it like this:

cat textfile | termsql -d ',' -o sqlite.db

alternatively you can do this:

termsql -i textfile -d ',' -o sqlite.db

By default it will generate column names "COL0", "COL1", if you want it to use the first row for the columns names you do this:

termsql -i textfile -d ',' -1 -o sqlite.db

If you want to set custom column names you do this:

termsql -i textfile -d ',' -c 'id,name,age,color' -o sqlite.db

使用Sqlitebrowser http://sqlitebrowser.sourceforge.net/中的导入功能已经很幸运了

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM