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.
Related
I'm trying to add rows from a CSV file to an existing table.
In Mac Terminal, I start sqlite3:
sqlite> .open database.db
sqlite> .import test.csv table
.
.
.
test.csv:43200: expected 22 columns but found 1 - filling the rest with NULL
sqlite>
Notice there are 43200 rows in the csv file. The file itself has 22 comma separated columns, but sqlite only thinks there is 1 column.
According to the finds of Cgaters and the official documentation of SQLite:
Use the ".import" command to import CSV (comma separated value) data into an SQLite table. The ".import" command takes two arguments which are the name of the disk file from which CSV data is to be read and the name of the SQLite table into which the CSV data is to be inserted.
Note that it is important to set the "mode" to "csv" before running the ".import" command. This is necessary to prevent the command-line shell from trying to interpret the input file text as some other format.
Example:
sqlite> .mode csv
sqlite> .import C:/work/somedata.csv tab1
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
Everytime I start SQLite, I have to re-turn on headers, re-switch to column mode, re-change the separator and/or width. How can I make the settings persist??
Repeated Code
.mode column
.headers on
.separator ','
In other words, how can I save the settings so that next time I run SQLite, my preferences are automatically applied.
Create a .sqliterc file in your home directory
Ah, finally stumbled upon the answer. You can create a .sqliterc (SQLite Run Commands file) in your home directory. Every time you run sqlite, it will load the settings from the rc file.
Here's a quick command line script you can paste in the terminal for some basic settings (edit it to add additional preferences):
cat << EOF > ~/.sqliterc
.headers on
.mode column
EOF
There are alternative command line options. Something like this:
alias mysqlite='sqlite3 -column -header -separator ,'
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
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.