Import .CSV files onto SQLite - sqlite

I am having some problem in importing a .csv file into a sqlite table. My .csv file looks like this
Name,Age,,
Hamish,27,,
Praveen,27,,
There are no trailing spaces anywhere. I create a table in the SQLite db with the same schema, but when i run .import...it shows me an error saying "expected 2 columns of data but found 1". Probably something to do with the delimiter. Any ideas?

Make sure you specify the delimiter, and do so without quotes:
.separator ,

Related

sqlite3 is not using end-of-line character when importing data

I have a tab-delimited file which I'm attempting to load into a table. The table has already been created and structured appropriately, the challenge is that SQLite3 is combining the last value on one row with the first value on the next row.
So for a file where the last line was SomeText, and the next line begins with 12345, the value imported is SomeText12345
Right now I'm using the following command:
.separator "\t";
.import MyFile.tsv MyTable
Any ideas how I can get the data to load while recognizing the end-of-line?
I noticed the same problem. I've always suspected it had to do with the last value in a tab-separated file being a TEXT type. A little stack-sniffing turned up this post wherein the second answer says:
There is actually a dedicated mode for importing tab separated files:
sqlite> .mode tabs
sqlite> .import MyFile.tsv MyTable

How Can I Properly Export data from DB in which some values have special character like "\r"?

I have a table on my DB and one of which columns has some special characters like "\r"(enter). Maybe these were done by typist who surveyed this data. This column was originated from essay question, in my opinion.
The problem is this. Because of situation above, some cells have special characters.
With DB tool, export table into Excel file does not go wrong. But export it to delimited file like CSV is different, even in R write.table. Some character ( "\r") does something; It make another line; 69297 → 69454.
So is there a way to handle this things??

Is it possible to import a CSV file to an existing table without the headers being included?

I'm trying to import a CSV file to a table that is empty but already exists in an SQLite database. For example:
sqlite> CREATE TABLE data (...);
sqlite> .mode csv
sqlite> .import mydata.csv data
I have created the table in advance because I'd like to specify a primary key, data types, and foreign key constraints. This process works as expected, but it unfortunately includes the header row from the CSV file in the table.
Here's what I've learned from the SQLite docs regarding CSV imports:
There are two cases to consider: (1) Table "tab1" does not previously exist and (2) table "tab1" does already exist.
In the first case, when the table does not previously exist, the table is automatically created and the content of the first row of the input CSV file is used to determine the name of all the columns in the table. In other words, if the table does not previously exist, the first row of the CSV file is interpreted to be column names and the actual data starts on the second row of the CSV file.
For the second case, when the table already exists, every row of the CSV file, including the first row, is assumed to be actual content. If the CSV file contains an initial row of column labels, that row will be read as data and inserted into the table. To avoid this, make sure that table does not previously exist.
So basically, I get extra data because I've created the table in advance. Is there a flag to change this behavior? If not, what's the best workaround?
The sqlite3 command-line shell has no such flag.
If you have a sufficiently advanced OS, you can use an external tool to split off the first line:
sqlite> .import "|tail -n +2 mydata.csv" data
You can also use the --skip 1 option with .import as documented on the sqlite3 website and this SO Answer. So, you can use the following command
.import --csv --skip 1 mydata.csv data

SQLITE 3.9.0, Windows 10 x64 - How to import a .csv from the command line shell?

I can do it using the GUI (not that hard) but I really would like do to it by sqlite command lines. I've googled it and have tried everything, however nothing seems to work. Please give me a hint on this! This is the last thing I've tried:
CREATE TABLE 'teste3' (
'Id' integer,
'Idade' integer,
'Sexo' text,
'Peso' integer
);
.separator ',';
.mode csv;
.import 'C:\Users\xxxx\Documents\Monografia\base_teste.csv' teste3
What I intended to do was to create a table ('teste3',done) and them "fill it" by importing a given .csv file. Instead, I keep getting this error message: "near ".": syntax error:". Then I tried to cut off the "." before separator, for example, but I got another error: "near "separator": syntax error:". I really don't know what to do. Thanks!
Dot commands like .mode and .import are not SQL statements; they are implemented by the sqlite3.exe command-line shell (which can be downloaded from the offical SQLite site).
The DB Browser for SQLite is an entirely independent tool. It does not implement these dot commands; you have to use the GUI instead.

CSV file is not recognized

Im exporting an excel file into a .csv file (cause I want to import it into R) but R doesn't recognize it.
I think this is because when I open it in notepad I get:
Item;Description
1;ja
2;ne
While a file which does not have any import issues is structured like this in notepad:
"Item","Description"
"1","ja"
"2","ne"
Does anybody know how I can either export it from excel in the right format or import a csv file with ";" seperator into R.
It's easy to deal with semicolon-delimited files; you can use read.csv2() instead of read.csv() (although be aware this will also use comma as the decimal separator character!), or specify sep=";".
Sorry to ask, but did you try reading ?read.csv ? The relevant information is in there, although it might admittedly be a little overwhelming/hard to sort out if you're new to R:
sep: the field separator character. Values on each line of the
file are separated by this character. If ‘sep = ""’ (the
default for ‘read.table’) the separator is ‘white space’,
that is one or more spaces, tabs, newlines or carriage
returns.

Resources