SQLite - Joining 3 different databases at a single one - sqlite

I'm trying to Join tables from 3 different databases (A, B and C) which have columns in commom, but i got some issues. Each database has 1 table with the same name of the database.
First, I tried to do that using VSCode (sqlite3 extension) by attaching the databases B to A:
ATTACH 'A.db' AS db1;
ATTACH 'B.db' AS db2;
SELECT * FROM db1.A
INNER JOIN db2.B ON B.ColumnInCommon = A.ColumnInCommon;
I got the following error:
No such table: db1.A
Could anyone help me with another solution?

I believe that you have to believe what the message says. The following demonstrates that what you are doing does work:-
sqlite> .open 'C.db'
sqlite> ATTACH 'A.db' AS db1;
sqlite> ATTACH 'B.db' AS db2;
sqlite> PRAGMA database_list;
0|main|C:\Users\Mike\C.db
2|db1|C:\Users\Mike\A.db
3|db2|C:\Users\Mike\B.db
sqlite> CREATE TABLE IF NOT EXISTS main.C (ColumnInCommon);
sqlite> CREATE TABLE IF NOT EXISTS db1.A (ColumnInCommon);
sqlite> CREATE TABLE IF NOT EXISTS db2.B (ColumnInCommon);
sqlite> SELECT * FROM db1.A INNER JOIN db2.B ON B.ColumnInCommon = A.ColumnInCommon;
sqlite>
i.e. no error.
I can force an error using :-
sqlite> SELECT * FROM dbx.A INNER JOIN db2.B ON B.ColumnInCommon = A.ColumnInCommon;
Error: no such table: dbx.A
sqlite>
But this shouldn't apply as your ATTACH appears to have worked (pragma database_list; will confirm as used above). As such it is probably that the table itself does not exist. I'd suggest trying :-
SELECT * FROM db1.sqlite_master;
This would then show the tables in the A(db1) database e.g. :-
sqlite> SELECT * FROM db1.sqlite_master;
table|A|A|2|CREATE TABLE A (ColumnInCommon)
sqlite>

Related

Creating a sqlite table in terminal (iOS)

sqlite> create table t
...> SELECT * FROM sqlite_master WHERE type='table' AND name='t';
Error: near "SELECT": syntax error
sqlite>
I'm trying to create a table called 't' in terminal. I'm getting the error. Maybe I'm missing somewhere?
The syntax you need to use here is CREATE TABLE AS ... SELECT:
> CREATE TABLE t AS SELECT * FROM sqlite_master WHERE type = 'table' AND name = 't';

bulk update in SQLite

I have 2 tables with identical structure I want to update one table using data from the other, matching on primary key. SQLite has a with (CTE) statement but the following doesn't work (sqlite3 v. 3.29.0):
sqlite> select * from main;
1|A
2|B
4|D
5|E
6|F
sqlite> select * from temp;
1|aa
2|bb
3|cc
4|dd
5|ee
sqlite> with mapping as (select main.ID, temp.Desc from main join temp on temp.ID=main.ID) update main set Desc=mapping.Desc where main.ID=mapping.ID;
Error: no such column: mapping.Desc
I've tried using "select main.ID as ID, temp.Desc as Desc", but get the same error message.
To update your main table from your cte, use a subquery, since sqlite doesn't support update from
with mapping as
(select main.ID, temp.Desc
from main
join temp on temp.ID=main.ID)
update main set Desc=
(select Desc from mapping where ID = main.ID limit 1);
see dbfiddle

how to get the ordered query in sqlite?

I insert some data into test table.
C:\> sqlite3 e:\\test.db
sqlite> create table test(code TEXT,content numeric);
sqlite> insert into test(code,content)values('x',3);
sqlite> insert into test(code,content)values('x',1.5);
sqlite> insert into test(code,content)values('x',1);
sqlite> insert into test(code,content)values('y',9);
sqlite> insert into test(code,content)values('y',3);
sqlite> insert into test(code,content)values('y',2);
sqlite> insert into test(code,content)values('y',12.3);
sqlite> insert into test(code,content)values('y',11.5);
how can i get the ordered output as the following ?
select * from test group by code order by content;can not get it.
select * from test order by content;neither.
x|1
x|1.5
x|3
y|2
y|3
y|9
y|11.5
y|12.3
You can sort by multiple criteria:
SELECT * FROM test ORDER BY code, content;

Why do I get the error "Error: no such column: main.<tablename>.<columname>" although it exists?

This works:
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main /path/to/db
2 uni /path/to/db
also this:
sqlite> pragma main.table_info(tsv_storage);
0|id|int|0||0
1|seqid|text|0||0
...
and this:
sqlite> select count(*) from main.tsv_storage;
198159
and also the attached database works:
sqlite> select * from uni.fasta_storage where uni.fasta_storage.id = 1;
1 MASNTVSAQ... Q197F8.1 002R_IIV3 Uncharacterized protein 002R Q197F8
but this not:
sqlite> select main.tsv_storage.seqid where main.tsv_storage.id=8;
Error: no such column: main.tsv_storage.seqid
EDIT:
and I have also problems with this, do I have to join the tables?
insert into main.tsv_storage(seqlength) select length(fasta) from
uni.fasta_storage where uni.fasta_storage.title = main.tsv_storage.seqid;
Error: no such column: main.tsv_storage.seqid
It happens for all columns, not only seqid. I think I did everything that is explained here: http://sqlite.awardspace.info/syntax/sqlitepg12.htm
What am I missing?
sqlite> select * from main.tsv_storage.seqid where main.tsv_storage.id=8;
You have not defined where to look for the selection. You need to tell the query what fields to search within the table, then define which table you are searching. The select * portion tells the query to look in all fields within the table. The from portion of the query tells the processes what table to look in. And lastly the where portion tells the query what to match when looking.
When using INSERT ... SELECT ..., the SELECT part must be valid query.
You cannot access a column like main.tsv_storage without having the table in the FROM clause:
INSERT INTO main.tsv_storage(seqlength)
SELECT length(fasta)
FROM uni.fasta_storage, main.tsv_storage
WHERE uni.fasta_storage.title = main.tsv_storage.seqid;
And the entire commands looks suspicious.
Are you sure you don't want to update the values in the seqlength column for existing records?
In that case, you would use something like this:
UPDATE main.tsv_storage
SET seqlength = (SELECT length(fasta)
FROM uni.fasta_storage
WHERE uni.fasta_storage.title = main.tsv_storage.seqid);

How can I get the list of a columns in a table for a SQLite database?

I am looking to retrieve a list of columns in a table. The database is the latest release of SQLite (3.6, I believe). I am looking for code that does this with a SQL query. Extra bonus points for metadata related to the columns (e.g. length, data type, etc...)
What you're looking for is called the data dictionary. In sqlite a list of all tables can be found by querying sqlite_master table (or view?)
sqlite> create table people (first_name varchar, last_name varchar, email_address varchar);
sqlite> select * from sqlite_master;
table|people|people|2|CREATE TABLE people (first_name varchar, last_name varchar, email_address varchar)
To get column information you can use the pragma table_info(table_name) statement:
sqlite> pragma table_info(people);
0|first_name|varchar|0||0
1|last_name|varchar|0||0
2|email_address|varchar|0||0
For more information on the pragma statements, see the documentation.
Here's the simple way:
.schema <table>
The question is old but the following hasn't been mentioned yet.
Another convenient way in many cases is to turn headers on by:
sqlite> .headers on
Then,
sqlite> SELECT ... FROM table
will display a headline showing all selected fields (all if you SELECT *) at the top of the output.
Here's a SELECT statement that lists all tables and columns in the current database:
SELECT m.name as tableName,
p.name as columnName
FROM sqlite_master m
left outer join pragma_table_info((m.name)) p
on m.name <> p.name
order by tableName, columnName
;
just go into your sqlite shell:
$ sqlite3 path/to/db.sqlite3
and then just hit
sqlite> .schema
and you will get everything.
This is a query that lists all tables with their columns, and all the metadata I could get about each column as OP requested (as bonus points).
SELECT
m.name AS table_name,
p.cid AS col_id,
p.name AS col_name,
p.type AS col_type,
p.pk AS col_is_pk,
p.dflt_value AS col_default_val,
p.[notnull] AS col_is_not_null
FROM sqlite_master m
LEFT OUTER JOIN pragma_table_info((m.name)) p
ON m.name <> p.name
WHERE m.type = 'table'
ORDER BY table_name, col_id
Thanks to #David Garoutte for showing me how to get pragma_table_info to work in a query.
Run this query to see all the table metadata:
SELECT * FROM sqlite_master WHERE type = 'table'
Building on the above, you can do it all at once:
sqlite3 yourdb.db ".schema"
That will give you the SQL to create the table, which is effectively a list of the columns.
In case if you want to get all column names into one single comma separated string, you can use below.
SELECT GROUP_CONCAT(NAME,',') FROM PRAGMA_TABLE_INFO('table_name')
Here the pragma table_info is used as pragma_table_info for the select statement and GROUP_CONCAT is to combine all the field names into one string. for the second parameter of GROUP_CONCAT you can pass the separator.
I know, it’s been a long time but it’s never too late…
I had a similar question with TCL as interpreter and after several search, found nothing good for me. So I propose something based on PRAGMA, knowing that your DB is “main”
db eval { PRAGMA main.table_info(<your table name>) } TBL { puts $TBL(name) }
And array use to obtain a list
set col_list {}
db eval { PRAGMA main.table_info(<your table name>) } TBL { lappend col_list $TBL(name) }
puts $col_list

Resources