How to save query results to a new sqlite? - sqlite

I want to save the results of my sqlite query in a new sqlite file. in other words, I want to have a new sqlite database with the data which resulted from my query on my previous data set.
All I have found on the internet was how to export the query results to csv or sql. but I want my new data set to be sqlite.
Also, is there any way to save the query results on the same data set (like what we do in excel)? Thank you!

You can ATTACH another database file to an existing connection and create or insert data into a table in that other database.
Something like:
ATTACH DATABASE 'other.db' AS other;
CREATE TABLE other.foo AS SELECT * FROM main.foo; -- create and populate a table
INSERT INTO other.bar SELECT * FROM main.foo; -- insert into an existing table
DETACH other;

Related

How to do a batch update in sqlite3 database

All:
I am pretty new to SQL, I wonder how can I update certain field value across multiple tables in SQLITE3 database?
For example:
the database is company.db, inside it, there are 50 tables, each table has a column called company_name, now some company's names changed, so I need to update that info in all tables, I wonder how to do it in SQL?
Thanks

teradata sql to insert from a volatile table to a permanent table

I have a requirement that I need to store a list of parameters. The parameters will be input as a csv file. They will be inserted into a table if not already existing. I have got a suggestion that I can import this data into a volatile table and use a sql query like:
insert into table permvariables
select * from tempvariables
minus
select * from permvariables;
Where tempvariables is my volatile table and permvariables is my permanent table. Will this solution work? Is there a better way to do it?
Instead of MINUS simply use a
MERGE INTO permvariables AS tgt
USING tempvariables AS src
ON tgt.pk_column(s) = src.pk_column(s)
WHEN NOT MATCHED INSERT VALUES (src.pk_column(s), src.cola, ...)

Copy SQLite table including metadata

I wanted to add a constraint to an existing column in my SQLite database. However, I read that it is not possible to do so.
I tried the solution from How do I rename a column in a SQLite database table?, but there seems to be missing the copying of all the metadata.
I pretty much want an exact copy of a given table, except for the new constraints.
How does the INSERT command look like to copy all the metadata, thus the indexes will increase correctly, for example.
I'm not a heavy user of sqlite3, but you can use the command line to get the data and "create table" and "create index" commands. I am using the 'History' DB from the Google chrome browser which has a table called "visits". The 'mode insert' command says to provide output in a format that can be used to input this data. The '.schema visits' command says to show the 'create table' and 'create index' statements. The 'select..' statement gives you the data. The database I used doesn't seem to have any foreign key constraints, but they could very well be part of the 'create table' information if your DB has any.
sqlite3 History
.mode insert
.schema visits
select * from visits;

create table with constraints from another table in sqlite?

I want create table from another table with constraint?
I used this query "create table destination as select * from source;" fro table creation.
But its copy only the column name in table without column constraint.
There is a special table named sqlite_master, holding the full CREATE TABLE statement for each table (it's modified as appropriate during ALTER TABLE).
I would make my application retrieve that CREATE TABLE statement:
SELECT sql FROM sqlite_master WHERE type='table' AND name='source';
Then I would replace the table name right after CREATE TABLE tokens, and execute the result as a new sqlite query.
I don't think that it's possible to do in sqlite's pure SQL without extensions.

SQLite Temp Table in Navicat

I use Navicat and this command to create temp table in sqlite:
create temp table search as select * from documents
Then when i try to query:
select * from search
I got:
no such table: temp.sqlite_master
or:
no such table
The table doesn't appear in table list too, but when I try to create it again I get:
table search already exists
What is the problem? is it from navicat?
You create statement looks correct to me. When you create a temp table it is deleted when you close the connection string used to create the table. Are you closing the connection after you create the table and then opening it again when you are sending the query?
If not, can you include your query statement too?
It's like a bug in SQLite DLL shipped with Navicat. Test it somewhere else worked ok.
Documentation of SQLite tells this about CREATE TABLE:
If a is specified, it must be either "main", "temp",
or the name of an attached database. In this case the new table is
created in the named database. If the "TEMP" or "TEMPORARY" keyword
occurs between the "CREATE" and "TABLE" then the new table is created
in the temp database. It is an error to specify both a
and the TEMP or TEMPORARY keyword, unless the is
"temp". If no database name is specified and the TEMP keyword is not
present then the table is created in the main database.
May be you should accesse table via temp prefix like this: temp.search.

Resources