First column of sqlite table throwing error: no such column - sqlite

This seems odd to me and violates my previous experience with SQLite. I have a trivial database with one table and 3 columns. When I query the first column I get 'Error: no such column: Name'. The other columns work fine. Log below. There must be something ridiculously basic that I am missing. My sleazy work-around is to add a dummy first column, at which point querying on 'Name' works fine, but that is pretty unsatisfactory. Running on a Mac mini (M1, 2020), macOS Monterey Version 12.5.1.
bash-3.2$ head Names/nameTest1.csv
Name,Gender,Count
James,M,5304407
John,M,5260831
Robert,M,4970386
...
bash-3.2$ sqlite3 test1.db
SQLite version 3.19.3 2017-06-08 14:26:16
Enter ".help" for usage hints.
sqlite> .mode csv
sqlite> .separator ','
sqlite> .import Names/nameTest1.csv testtable1
sqlite> .schema
.schema
CREATE TABLE testtable1(
"Name" TEXT,
"Gender" TEXT,
"Count" TEXT
);
sqlite> select * from testtable1 where Count='5304407';
James,M,5304407
sqlite> select * from testtable1 where Name='James';
Error: no such column: Name
sqlite> .quit
.quit

Your CSV file has the BOM field. It does not correspond to a printable character, so you do not see it, but SQLite fails to strip it and includes it as the the prefix of your Name field. This is a known issue, but recent versions of SQLite should handle it properly. You might be using an older version. Either upgrade your SQLite or strip the BOM code before importing.

Related

Why does SUM(mycolumn) display decimal point?

In an SQLite database, I created a table and imported CSV data into it.
The last column only contains integers:
ZIP;AMOUNT
78123;4272
95456;154
etc.
I used the following commands:
.mode csv
.separator ';'
CREATE TABLE MyTable("ZIP" TEXT, "AMOUNT" INTEGER);
.import input.csv MyTable
sqlite> select SUM(AMOUNT) from MyTable;
25270.0
Why is SQLite displaying SUM with a decimal?
Thank you.
===
Edit: Here's the infos:
sqlite> select typeof(AMOUNT) from MyTable LIMIT 10;
text
integer
integer
integer
integer
etc.
sqlite> select typeof(SUM(AMOUNT)) from MyTable;
real
==
Edit: Here's the top of input.csv as exported from LibreOffice Calc:
ZIP;AMOUNT
78123;4272
95456;154
etc.
Maybe I didn't use the right commands to import data into SQLite.
#PetSerAl has it. Because you're importing into an existing table, every line is imported including the header. That string makes sum() return a floating point result.
I work around this case with:
.import '| tail -n +2 input.csv' mytable
which strips the first line.
If the first character of the filename is a |, it's treated as a shell command to run (Using the C popen() function) and its output is used as the data to import.

sqlite3 .import failing to create table

I have seen other posts and tried the answers but it still will not create the table for me.
I simply want to import a .tab file into a new sqlite3 table on the fly.
I have tried .mode csv with a .separator '\t' and .mode tabs
I have the following command and error.
sqlite> .mode tabs
sqlite> .import /storage/mgymrek/gtex-estrs/revision/mastertables/Adipose-Subcutaneous_master.csv Temp1
Error: no such table: Temp1
I have also tried
sqlite3 -separator '\t' estr.db ".import Adipose-Subcutaneous_master.csv Temp1"
Error: no such table: Temp1
Please help!
To import a CSV file in SQLit3, set the mode to csv. If it's a tab-seperate-file, you could try importing the file as shown in: How to import a tsv file with SQLite3
sqlite> .mode csv
sqlite> .import /storage/mgymrek/gtex-estrs/revision/mastertables/Adipose-Subcutaneous_master.csv Temp1
As per this answer, if you have an older version of SQLite this will happen. I'm not sure in which version the automatic creation of a table was implemented, but from that post if you have SQLite 3.8.9 or newer it should automatically create the table, possibly a few versions before would also do that. If you have 3.7.17 or older it won't create the table, this is the version I have and it doesn't work (I arrived here investigating why). I don't know the versions in-between, but if you're there and it's not working for you I suggest you upgrade.

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

showing "table" as an invalid syntax in Python 3.5 using sqlite3

Here's what I am trying to do:
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute(CREATE table Movies(index integer, mvnm text, year integer, genere text))
SyntaxError: invalid syntax
It highlights table in red color.
There are few problems:
The SQL should be provided in quotes because the execute() method accepts a
string argument.
index is a reserved keyword in SQL, so you cannot use
that as the name of your column.
If you want to run this script
repeatedly, you should add IF NOT EXISTS clause. Otherwise
consequent runs will fail due to "table already exists"
So, after these changes the statement looks like below:
c.execute("CREATE table IF NOT EXISTS Movies(myindex integer, mvnm text, year integer, genere text)")
You can then verify the table creation by logging in to sqllite:
$> ~ $ sqlite3 example.db
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite> .tables
Movies

Error when using changing mode to csv

I am trying to import data from a csv file.
According to this , I should use .mode csv first. But when I enter that command, I get this error:
mode should be on of: column html insert line list
Why is this so ?
Google didn't help me much and I'm new to this.
You had probably entered on the SQLite shell using sqlite. Try to use sqlite3 to enter on the shell. There you will have the .mode csv.
I entered the shell with sqlite3 and got the same error when I tried to switch modes. I found out the problem was dumber then sqlite vs. sqlite3, I am new to this so newbie error lol
sqlite> .mode column;   will give the error mentioned ^
sqlite> .mode column  without the ; won't
you can check if the mode actually switched by just running .mode and it will tell you what mode it is on

Resources