Creating a sqlite table in terminal (iOS) - sqlite

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';

Related

Verify if an index exists in a Sqlite table

I want to verify if an index exists for a SQL table. I'm trying this command (from here) :
SELECT *
FROM sys.indexes
WHERE name='YourIndexName' AND object_id = OBJECT_ID('Schema.YourTableName')
But I'm getting the error :
OperationalError: no such table: sys.indexes
with sqlite3 in Python.
SELECT
*
FROM
sqlite_master
WHERE
type= 'index' and tbl_name = 'your_table_name' and name = 'your_index_name';
You can use above query to verify index exist for a specific table and specific name.

SQLite - Joining 3 different databases at a single one

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>

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

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 to get list of all the tables in sqlite programmatically

How can I get the list of all the available tables in sqlite programmatically?
try this :
SELECT * FROM sqlite_master where type='table';
Use the below sql statement to get list of all table in sqllite data base
SELECT * FROM dbname.sqlite_master WHERE type='table';
The same question asked before on StackOverFlow.
How to list the tables in an SQLite database file that was opened with ATTACH?
worked for me
SELECT * FROM sqlite_master where type='table'
con = sqlite3.connect('db.sqlite')
cur = con.cursor()
cur.execute('''
SELECT tbl_name
FROM sqlite_master
WHERE type = 'table';
''')
print(cur.fetchall())

Resources