A
A
Alexander Vishnev2021-11-26 15:11:45
SQLite
Alexander Vishnev, 2021-11-26 15:11:45

How can I import data into a SQLite DB from a text file?

There is a SQLite database with fields: id, theme, question, answer
There is also a text document containing text (example):
Who is an elephant? | Elephant is a circle
Who is a goose? | A goose is a goose
, and so on. +20000 lines.... How
can I import this text file into the database?
What would question = Who is the elephant? answer = Elephant has a circle, etc.
id= ai, other fields are empty.

I tried the SQLite Studio program, but the import did not work.
Helped by Navicat for SQLite. Is there anything free with similar functionality?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2021-11-26
@fromdns

Bash import example

echo -e "question|answer\nКто есть слон? | Слон есть круг" | sqlite3 -cmd ".mode list" my.db ".import /dev/stdin my_table"

Here is what is stored in the database with the command
sqlite3 answers.db .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE tbl1(
  "question" TEXT,
  "answer" TEXT
);
INSERT INTO tbl1 VALUES('Кто есть слон? ',' Слон есть круг');
COMMIT;

If you remove the spaces around the pipe "|" , then it imports as it should.
Added
Or just by specifying the path to the file
sqlite3 -cmd ".mode list" my.db ".import myfile.txt my_table"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question