Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load contents in text files to sqlite table?

Tags:

sqlite

I have simple text files that contain just normal texts.

I was wondering if there is a way to load the text contents to table in sqlite.

So maybe I could create table myTable(nameOfText TEXT, contents TEXT);

And then put the name of the text into the first column and contents to the second column.

If putting in the name of the file is hard, loading the content into one column table is just as fine.

Any suggestion would be appreciated.

Thank you !!

like image 497
Sardonic Avatar asked Mar 10 '13 01:03

Sardonic


People also ask

How do I import a text file into SQLite?

You can import a CSV file into SQLite table by using sqlite3 tool and . import command. This command accepts a file name, and a table name. Here, file name is the file from where the data is fetched and the table name is the table where the data will be imported into.

How do I display table contents in SQLite?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.

Can you import CSV into SQLite?

Importing files as CSV or other formats Use the ". import" command to import CSV (comma separated value) or similarly delimited data into an SQLite table. The ". import" command takes two arguments which are the source from which data is to be read and the name of the ...


2 Answers

Say you have a file text.txt of CSV format:

name1,content1
name2,content2

Try the commands below to import the data in test.txt into your table

D:\test>sqlite3 test.db
SQLite version 3.6.23
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table myTable(nameOfText TEXT, contents TEXT);
sqlite> .separator ","
sqlite> .import test.txt myTable
sqlite> select * from myTable;
name1,content1
name2,content2
sqlite>

Change the separator used in your text file with .separator command before you import the data.

like image 165
hflzh Avatar answered Sep 20 '22 14:09

hflzh


termsql is a tool that can convert text from a file or the output of a program (stdin) on-the-fly into a sqlite database.

termsql -c nameOfText,contents -i input.txt -o myDB.db 

This will create a table with the columns nameOfText and contents. For each line in input.txt one row will be inserted into myDB.db.

You didn't tell us about the delimiter nameOfText and the context are separated by. By default termsql assumes whitespace is the delimiter. But should it be ',' for example, then you would do something like this:

termsql -d ',' -c nameOfText,contents -i input.txt -o myDB.db

You can get termsql here: https://github.com/tobimensch/termsql

Termsql has other usecases, too. You can do SQL statements on the new database all in one command. The following example would create your database and return the nameOfText column on the command line for all rows where the contents column contains the string 'test'.

termsql -d ',' -c nameOfText,contents -i input.txt -o myDB.db "select nameOfText from tbl where contents like '%test'"
like image 28
user3573558 Avatar answered Sep 18 '22 14:09

user3573558