Combine two SQLite databases in bash - sqlite

I have two databases that are created in a bash file, let's call them 1.sqlite and 2.sqlite. I'd like to combine them into a new database (3.sqlite). Is there a command in bash that'll do that?
Edit:
The answer that sputnick gave me got me on the right track. But I had to rearrange it to be:
sqlite3 1.sqlite .dump > tempdb.sqlite

Yes, try the following shell code:
sqlite3 .dump 1.sqlite > dump
sqlite3 .dump 2.sqlite >> dump
sqlite3 3.sqlite < dump
But take care of table collisions if they have the same names.

Related

Convert csv to db in sqlite

I have a csv file with ; separator. I would like to convert it into .db. I follow advice here: https://www.sqlite.org/cli.html#section_8
In my Ubuntu terminal I use:
sqlite3 ex1
Then I try:
.mode list
.separator ";"
.import odberatel.csv odberatel
.save odberatel.db
But When I look on the odberatel.db in Db Browser for SQLite it seems that is not separated correctly:
What do I do wrong, please?
From the same sqlite doc:
Note that it is important to set the "mode" to "csv" before running the ".import" command.

SQLite importing csv file

I am using SQLite and I can't import this csv file for some reason. I ran cmd first then entered SQLite.
my input in cmd:
cd C:\Users\Fries\Desktop\SQLite
sqlite3
.mode csv
.import C:\Users\Fries\Desktop\city.csv cities
.schema cities
I am using this tutorial. http://www.sqlitetutorial.net/sqlite-import-csv/
for some reason I can't open the csv
I am new to dbms and sqlite seems to be a super simple one
Going by the feedback in the error message, it looks like the backslashes need to be escaped. I would first try this:
sqlite> .import C:/Users/Fries/Desktop/city.csv
The following might also work, using escaped backslashes:
sqlite> .import C:\\Users\\Fries\\Desktop\\city.csv

Is it possible to append the current date to filename is sqlite?

I want the filename to be something like this top_queries_June.csv. Is it possible to do that in sqlite?
sqlite>.output (top_Queries + Select date('now')).csv
I run this batch file to create a file with the top queries.
Batch file:
sqlite3 mydb.db ".read x.sql"
x.sql:
.headers ON
.mode csv
.output Top_Queries + <current month>.csv
select * from query;
.quit
As #CL. already pointed out the sqlite3 command line utility is not flexible enough to do this. You appear to be using some kind of shell to execute sqlite3. Most shells support a feature called "here document" and the sqlite3 utility supports reading commands from a pipe. Using these you can inline the x.sql file into your shell script. In a POSIX compatible shell your code could look like this:
DATE=`date -I`
sqlite3 mydb.db <<EOF
.headers ON
.mode csv
.output Top_Queries${DATE}.csv
select * from query;
EOF

Loop SQLite query for many databases in one directory to sort positive hits (UNIX)

I have a directory with lots and lots of SQLite database files.
I need to run an sqlite3 query to establish if any database in my directory contains a specific table. Database filenames go like 001.db, 002.db, etc. If a database contains the table I require, the file is copied to a different directory.
My initial thoughts are similar to this:
while [ sqlite3 $* "SELECT * FROM table1" != "Error: no such
table: table1" ]`
do
cp $* newdir/
done
or something similar, and I need some help with correct syntaxis please...
in "$*" I meant a database file currently being processed inside the directory, not sure if it is correct...
First, you don't need to do a select in a table to see if it exists. You could run something like:
SELECT * FROM dbname.sqlite_master WHERE type='table';
or adapt it to do a count:
SELECT count(*) FROM dbname.sqlite_master WHERE type='table' and name='table_name';
Using the sqlite command line, you should get a "1" or a "0". You can use that in figuring out if the DB actually has your desired table or not and act accordingly. The script could look something like:
for i in `ls *.db`; do
HAS_TABLE=`sqlite "${i}" "select count(*) ....;"
if [[ ${HAS_TABLE} == "1" ]]; then
cp ${i} some/other/dir/${i}
fi
done
I'm writing this from memory, so the syntax of the if/condition may be off a bit (you might be ok without the quotes around the 1 value).
HTH,

Export from sqlite to csv using shell script

I'm making a shell script to export a sqlite query to a csv file, just like this:
#!/bin/bash
./bin/sqlite3 ./sys/xserve_sqlite.db ".headers on"
./bin/sqlite3 ./sys/xserve_sqlite.db ".mode csv"
./bin/sqlite3 ./sys/xserve_sqlite.db ".output out.csv"
./bin/sqlite3 ./sys/xserve_sqlite.db "select * from eS1100_sensor_results;"
./bin/sqlite3 ./sys/xserve_sqlite.db ".exit"
When executing the script, the output apears on the screen, instead of being saved to "out.csv". It's working doing the same method with the command line, but I don't know why the shell script fails to export data to the file.
What am I doing wrong?
Instead of the dot commands, you could use sqlite3 command options:
sqlite3 -header -csv my_db.db "select * from my_table;" > out.csv
This makes it a one-liner.
Also, you can run a sql script file:
sqlite3 -header -csv my_db.db < my_script.sql > out.csv
Use sqlite3 -help to see the list of available options.
sqlite3
You have a separate call to sqlite3 for each line; by the time your select runs, your .out out.csv has been forgotten.
Try:
#!/bin/bash
./bin/sqlite3 ./sys/xserve_sqlite.db <<!
.headers on
.mode csv
.output out.csv
select * from eS1100_sensor_results;
!
instead.
sh/bash methods
You can either call your script with a redirection:
$ your_script >out.csv
or you can insert the following as a first line in your script:
exec >out.csv
The former method allows you to specify different filenames, while the latter outputs to a specific filename. In both cases the line .output out.csv can be ignored.
I recently created a shell script that will be able to take the tables from a db file and convert them into csv files.
https://github.com/darrentu/convert-db-to-csv
Feel free to ask me any questions on my script :)
Although the question is about shell script, I think it will help few of those who are just bothered about transferring the data from the sqlite3 database to a csv file.
I found a very convinient way to do it with the firefox browser using SQLite Manager extension.
Simply connect to your sqlite database file in firefox ( SQlite manager -> connect database ) and then Table -> Export table. You will be served with some more options that you can just click and try....
In the end you get a csv file with the table u have chosen to be exported.
Using command line for Linux:
user#dell-Admin: sqlite3 #activate your sqlite database first
sqlite> .tables #search for tables if any available if already created one.
sqlite> .schema #if you want to check the schema of the table.
# once you find your table(s), then just do the following:
sqlite> .headers on #export along with headers (column names)
sqlite> .mode csv #file type is csv
sqlite> .output example.csv #you want to provide file name to export
sqlite> SELECT * from events; #If entire table is needed or select only required
sqlite> .quit #finally quit the sqlite3
Now search in your system for example.csv file and you will get it.
In one line is
sqlite3 -header -csv ./sys/xserve_sqlite.db "select * from eS1100_sensor_results;" >./out.csv
A synthesis of the answers till now:
function sqlite2csv-table() {
local db="${1}" table="${2}" output="${3}"
if test -z "$output" ; then
output="${db:r}_hi${table}.csv"
fi
[[ "$output" =~ '.csv$' ]] || output+='.csv'
echo "$0: outputting table '$table' to '$output'"
sqlite3 -header -csv "$db" "select * from ${table};" > "$output" || return $?
}
function sqlite2csv() {
local db="${1}" o="${2}"
tables=($(sqlite3 $db ".tables"))
local t
for table in $tables[#] ; do
sqlite2csv-table "$db" "$table" "${o}_${table}.csv"
done
}
Usage:
sqlite2csv some.db [/path/to/output]

Resources