How can I create the virtual table using FTS in SQLite - sqlite

How can I create the virtual table using FTS in SQLite.
I have 3 tables, but I don't want to add the columns manually into the table.
For example: I have the 3 tables
like: t1(t1_id,t1_name), t2(t2_id,t2_name), t3(t3_id,t1_id,t2_id,t3_name)
Now, I want to create virtual table in which I want to give only t3 and
the columns in t1 and t2 should automatically map into the virtual table.
The expected output should be:-
virtualTable(vt1,t3_id,t3_name,t2_id,t2_name,t1_id,t1_name)
I refer this and this links, but I didn't find the solution for my question

Related

merge secondary database into main one avoiding duplicate

I have two databases with the same structure. The first is the main one, while the second get updated periodically (in reality I have multiple "secondary" databases that I want to merge one by one into the main one).
The structure of the main and the secondary databases is identical.
I want to periodically dump all new values from the secondary database in the main one. However, the second time I do it, I want to exclude rows that were already copied the first time (and so on).
The tables in all these database have:
an ID column set as PRIMARY KEY going from 1 to N for each database (I suspect this was a mistake, but at the moment I can't change this)
a DATE column, representing a posix timestamp (float)
some other columns
My code looks like this:
ATTACH DATABASE secondary.db AS temp_db
DROP TABLE IF EXISTS my_table_temp
CREATE TABLE my_table_temp AS SELECT * FROM my_table
INSERT INTO main.my_table_temp SELECT * FROM temp_db.my_table
DELETE FROM my_table
INSERT INTO main.my_table SELECT DISTINCT * FROM main.my_table_temp ORDER BY date
DROP TABLE my_table_temp
the problem is that - I suspect due to the repeated ID column - the DISTINCT clause returns me:
UNIQUE constraint failed: my_table.id
However I don't care at all of the ID field that could also be dropped or reset.
NOTES:
the secondary databases are constantly updated by a code that - at the moment - I can't change
I initialize the "main" database copy-pasting one of the secondary to avoid regenerating the whole structure from scratch. Maybe there is a better way of doing this
Apologies if this is a naive question, but I'm very new with SQLite.
Thanks
Following the advice from #forpas, I solved this with the following code:
Assuming the columns to be id,date,col1 and col2
ATTACH DATABASE secondary.db AS temp_db
DROP TABLE IF EXISTS my_table_temp
CREATE TABLE my_table_temp AS SELECT date,col1,col2 FROM my_table
INSERT INTO main.my_table_temp SELECT date,col1,col2 FROM temp_db.my_table
DROP TABLE my_table /* I need to recreate my_table as I've removed a column*/
CREATE TABLE main.my_table AS SELECT DISTINCT date,col1,col2 FROM main.my_table_temp ORDER BY date
DROP TABLE my_table_temp
also, I automatized the extraction of the column names doing
SELECT name FROM PRAGMA_TABLE_INFO('my_table');
This is then passed to the python code running the script and the column id is removed from the list. Note that the second (and following) time I run this code, the column id won't be present in my_table to start with. However this approach allows the code to be the same in the two cases: either if the column id is there or not.
This procedure is then iterated over each table name to fully merge the two databases.

MariaDB: How can I select a temporary table if there is a persistent table with the same name?

I created a persistent table temp and a temporary table temp, that is, both have the same name. How can I use select/update/insert specifically to the persistent or the temporary table? How can I differ between them?
MariaDB Tutorial says:
Note − Temporary tables are permitted to have the same name as an existing non-temporary table because MariaDB views it as a difference reference.
So, I suppose it should be possible to refer to one of these tables. This question is related to this question I posed in SO, but goes one step back.
In case a temporary table has the same name as an existing non temporary table the temporary table will shadow the name of a non temporary table.
That means in a SQL statement you will not be able to reference the non temporary table.
A work around would be, to create a view on a non temporary table before creating the temporary table, since the view internally keeps the reference to the non temporary table:
CREATE TABLE t1 (a VARCHAR(100));
INSERT INTO t1 VALUES ("foo");
CREATE VIEW v_t1 AS SELECT a FROM t1;
CREATE TEMPORARY TABLE t1 (b VARCHAR(100));
INSERT INTO t1 VALUES ("bar");
SELECT * FROM v_t1;
SELECT * FROM t1;

SQLite: foreign key referencing data in an FTS5 shadow table

The full-text search extensions of SQLite (FTS3, FTS4, FTS5) create shadow tables. E.g., if I create FTS5-table
CREATE VIRTUAL TABLE test USING fts5(textData);
that will also automatically create several real (non-virtual) tables, with test_content among them, which (apparently) stores the actual data inserted into the original virtual table test.
I know SQLite authors suggest that these shadow tables "should not be accessed directly by the user". But it's not clear from the docs if there are no any guarantee at all about behavior of these tables, or this advise concerns primarily attempts to directly INSERT or UPDATE on them. But what are the risks of reading from these tables?
Specifically - I need another (regular) table to have a FOREIGN KEY which references rowid of the virtual FTS5 table:
CREATE VIRTUAL TABLE test USING fts5(textData);
CREATE TABLE myTable (col1 INTEGER REFERENCES test(rowid));
I couldn't find hints on that in the docs, but my own experiments showed that foreign key just doesn't work here - I still can delete records from the test table even though they are referenced from myTable. However, if instead I do
CREATE VIRTUAL TABLE test USING fts5(textData);
CREATE TABLE myTable (col1 INTEGER REFERENCES test_content(id));
then everything seems to work as needed - I can't delete records from the virtual test table if they are referenced from myTable.
This works because, as follows from direct inspection, the rowid value of the test table is always equal to the id column value of the test_content table. Even when I specify rowid manually like in INSERT INTO test (rowid, value) VALUES (424242, 'foobar'); - then a new row appears in the test_content with the corresponding content and the corresponding id equal to 424242 (even if no preceding records were ever in the table).
So basically, my question is - is this correspondence between the rowid of an FTS5-table and the id of the corresponding <name>_content shadow table guaranteed to hold? Or might this break in some cases or in future versions?

SQL Server Stored Procedure, selecting rows from multiple tables

I have a stored procedure that is pulling data from one table, now I have got the same data collected in a different table, as the IDs are different I cannot put all data from the second table into the first one.
So now I have to have two select statements in one stored procedure.
Although the corresponding data is same but the column names in both table are different.
For instance BRIEF_TITLE would be briefTitle in the second table.
How can I merge the data from two different tables into one?
The result is bonded to ASP.net grid view control.
As the comments above state, you need something like this:
select BRIEF_TITLE from t1
union all
select briefTitle from t2
This will give you a column name of BRIEF_TITLE, but if you want something else, then add an alias to the first select
select BRIEF_TITLE as ShortTitle from t1
union all
select briefTitle from t2

SQLite - select every row from all tables where a column name exists

I need to extract all rows from every table that has the column imgAssetURL to add to a pre loading system.
I think in essence something like:
SELECT imgAssetURL FROM *
What are my options?
The definitions for all tables are located in the sqlite_master table. You would have to read those definitions, figure out which tables have the column in it, and run a query on each of those.
See http://www.sqlite.org/fileformat2.html#sqlite_master

Resources