Extracting data from multiple sqlite databases to single .csv? - sqlite

I am using this to extract data from sqlite database file, commands.txt are where I put my sqlite query:
sqlite3 base1.db < commands.txt > "base1.csv"
This works OK for one file, now I need to apply that commands to multiple .db files at once. This .db files are stored inside subfolders (they all have the same structure, so that sqlite command work OK for all)
I need to collect data from all .db files based on sqlite query in commands.txt into one .csv file, if possible?
When I execute this, I dont get result but empty file.
cd /D "C:\sqlite-tools-win32-x86"
for /R %%G in (*.db) do (
sqlite3 < commands.txt > "%%~dpnG.csv"
)

Related

Run sqlite3 command inline

This may sound odd but I have 15 databases within different folders and I wish to update them all.
I have made a text file for me to easily copy the sql file that I need to import in to each root folder and load the DB, so I can just copy and paste each to CLI and then I can then type the following in to each one.
sqlite> .read backup.sql
and it imports the data I need it too...I then exit (CTRL + Z) and run the next one...
However, can I easily run something like
sqlite3 database.db > .read backup.sql
So I can effectively run the whole thing from one command line?

How to delete all files of a database in sqlite3?

How to delete all files of a database in sqlite3?
I try to delete the filename I created, but there is some strange files left.
If you sqlite3 database filename is "/xxx/test.db", you need try to delete follow four files:
"/xxx/test.db"
"/xxx/test.db-shm"
"/xxx/test.db-wal"
"/xxx/test.db-journal"
These four files may exist, may not exist , so you should ignore the error of "file not exist" during the unlinkat/unlink syscall.
From https://www.sqlite.org/tempfiles.html:
SQLite currently uses nine distinct types of temporary files:
Rollback journals
Master journals
Write-ahead Log (WAL) files
Shared-memory files
Statement journals
TEMP databases
Materializations of views and subqueries
Transient indices
Transient databases used by VACUUM
The following are always written in the same directory as the database file:
rollback journal (with the same name as the database file but with the 8 characters "-journal" appended)
WAL ("-wal" appended)
shared-memory file ("-shm" appended)
Master Journal File (randomized suffix)
Other temporary files can be located in the same directory, depending on a variety of factors.
See the above link for further details.
By request
If DBNAME is a pathname of the SQLite database, you might like to consider these options for removing all the related files in the directory in which the database file lives:
rm -i ${DBNAME} ${DBNAME}-*
or:
rm -i ${DBNAME}*
or if you're quite sure, either of the above but without -i

How do I send results of sql file to txt including queries run and comments in sqlite3?

I understand I can save my queries etc in an .sql file and run it against a database from the cmd prompt like this:
$ sqlite3 main.db < queries.sql > results.txt
but how can I write my .sql file to print comments or the actual statements themselves so that the results file is self explanatory when viewed?

Sqlite3: Location of the data base file in the file system

I am starting to get familiar with the sqlite. I am currently using SQLite version 3.13.0 2016-05-18.
I created a test data base called "test.db" as follows:
sqlite3 test.db
And then, I created a table called "employees" in the database as follow:
create table employees(id integer primary key, name text);
Then after I added the following entry to the table:
insert into employees(id, name) values(410,'test');
Everything works as expected. But, somehow I can not find where my test database file is located in the file system. Any idea where that .db file is located? I am in windows system.
I just realized that I had to save the .db file as follows:
.save test.db
And, then the .db file appeared in the same directory as SQLite database file is also a regular file.

How to use sqlite3.exe to create database from exported .sql script

Trying to convert an SqlCe database to SQLite, I export it to a .sql file. Now how would I use sqlite.exe to create a database from this .sql file?
Where to put the sql3.exe file?
What command syntax to use, in cmd prompt or in the sqlite.exe shell?
Use following command line:
sqlite3 -init dump.sql newsqlite.db ""
It will create new SQLite database file newsqlite.db by executing statements from dump.sql. Empty string "" is needed for sqlite3 to quit automatically.
If newsqlite.db file already existed with some data, import may fail unless you use IF NOT EXISTS for all table and index creation statements.
Put sqlite3.exe wherever you want, as long as you remember that place and you're able to start sqlite3 from there.
Applying a script to a database (maybe a newly-created one), in command prompt:
sqlite3.exe my-new-db.somesuffix < myscript.sql
Executing a script within interactive sqlite3 session:
sqlite3.exe my-new-db.somesuffix
....
.read myscript.sql
....
Both variants are valid and usable at times. (Note: if your .sql was generated for non-sqlite database, I'd expect that it will require some changes to work in sqlite3. And things like stored procedures and user-defined functions will be definitely lost).

Resources