How to select from sqlite table and dump to .sql file? - sqlite

I am trying to select records from a SQLite.db3 table and create a new insert.sql file.
these are the commands that I have used:
SQLite> .mode insert
SQLite> .output newFile.sql
SQLite> .dump
The problem I have is that I do not want to copy the entire db into newFile.sql. I only want to certain data from a table like: select * from table1 where status = 1

I have found a working solution:
sqlite> .open Corrupt.db3
sqlite> .mode insert
sqlite> .output NewTable.sql
sqlite> select * from OldTable where Status = 1;
sqlite> .output stdout

Related

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>

SQLite3: How to Import an CSV and change the column types?

I am looking to import an CSV using Command Line Shell For SQLite on linux (SQLite version 3.29.0), and set the appropriate data types.
sqlite> .open outputSQLDB.db
sqlite> .import input.csv tmpTable
But now the imported table is messed up:
sqlite> .schema
CREATE TABLE DX(
"id,field1,field2" TEXT
);
Why aren't the fields separated?
At the end do I just do:
sqlite> CREATE TABLE myTbl (
...> id INTEGER,
...> field1 TEXT,
...> field2 INTEGER
...> );
CREATE INDEX id_index on myTbl (id);
sqlite> DROP TABLE IF EXISTS tmpTable;
Specify .mode csv before inserting. Also make sure the temp table does not exist, otherwise SQLite interpretes the first line of the CSV as data.
Before dropping the temp table, transfer the rows to the new table with an INSERT INTO command. Otherwise they will be lost.
You will get a command sequence of:
.open outputSQLDB.db
DROP TABLE IF EXISTS tmpTable;
.mode csv
.import input.csv tmpTable
CREATE TABLE myTbl (
id INTEGER,
field1 TEXT,
field2 INTEGER
);
CREATE INDEX id_index on myTbl (id);
INSERT INTO myTbl (id, field1, field2)
SELECT id, field1, field2
FROM tmpTable;
DROP TABLE IF EXISTS tmpTable;
I would also either use
CREATE TABLE IF NOT EXISTS myTbl (
...
);
or
DROP TABLE IF EXISTS myTbl;
before creating the table.

SQLite: duplicate column error from Create Table

I'm a first time user for SQLite. I'm using command line SQLite, version 3.20.1 2017-08-24 16:21:36 on a Mac
I start SQLite by specifying the db file via the command line:
sqlite3 ~/www/sqlite/statistics.db
I try to create a table but receive the error duplicate column name
Advice appreciated. In the transcript below, I first tested with a 1 column table.
sqlite> CREATE TABLE api_methods(
...>    id INTEGER PRIMARY KEY
...> );
sqlite> SELECT name FROM sqlite_master WHERE type='table';
api_methods
sqlite> drop table api_methods;
sqlite> CREATE TABLE api_methods(
...>    id INTEGER PRIMARY KEY,
...>    action_name TEXT NOT NULL
...> );
Error: duplicate column name:   
sqlite>
Problem solved when I deleted the leading spaces before the column names:
sqlite> CREATE TABLE api_methods(
...> id INTEGER PRIMARY KEY,
...> action_name TEXT NOT NULL
...> );
sqlite> SELECT name FROM sqlite_master WHERE type='table';
api_methods
sqlite> pragma table_info(api_methods);
0|id|INTEGER|0||1
1|action_name|TEXT|1||0
sqlite>

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;

PRAGMA foreign_key_check;

I am trying to use PRAGMA foreign_key_check; without success :(
I have made some foreign keys violations on a database (with PRAGMA foreign_keys = OFF;), then I set this to true then launched PRAGMA foreign_key_check; but this did not return any results.
However, when I try to insert the very same lines with foreign keys violations with PRAGMA foreign_keys = ON; I do get a foreign key constraint violation error.
I am using SQLite 3.7.3.
Is there something I am missing regarding this command? Is there a bug?
Thanks
EDIT: I have just tried with 3.8.3.1 (higher version this time ;) ) but cannot get any results though :(
However, some other commands seem to not work as expected (especially .schema !?):
SQLite version 3.8.3.1 2014-02-11 14:52:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> attach "D:\MusicLib_Minimal_TEST_FOREIGN_KEYS.db" as db1;
sqlite> .schema device
sqlite> PRAGMA foreign_keys=ON;
sqlite> PRAGMA foreign_keys;
1
sqlite> PRAGMA foreign_key_check;
sqlite> select * from device
...> ;
1|test|/|/|0|0
sqlite> select * from playlist
...> ;
sqlite> PRAGMA integrity_check;
ok
sqlite> .dump device
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
COMMIT;
sqlite> PRAGMA table_info(device);
0|idDevice|INTEGER|1||1
1|name|TEXT|1||0
2|source|TEXT|1||0
3|destination|TEXT|1||0
4|idPlaylist|INTEGER|1||0
5|idMachine|INTEGER|1||0
sqlite>
PRAGMA foreign_key_check was added in sqlite 3.7.16. You have an older version.
Unknown pragmas are just no-ops; no errors are emitted.
I have found out the solution :) Problem was a misuse (or bug ?) of sqlite3.exe
When calling sqlite3.exe without parameters, a main empty database is created, and looks like the commands does not apply the attached db:
D:\>sqlite3.exe
SQLite version 3.8.3.1 2014-02-11 14:52:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> attach "D:\MusicLib_Minimal_TEST_FOREIGN_KEYS.db" as db1
...> ;
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main
2 db1 D:\MusicLib_Minimal_TEST_FOREIGN_KEYS.db
sqlite> .tables
db1.device db1.libType db1.optiontype db1.playlist
db1.file db1.machine db1.path db1.statement
db1.genre db1.option db1.playCounter db1.statsource
sqlite> .schema db1.device
sqlite> .exit
If opening database as an argument of sqlite3.exe, it works as expected:
D:\>sqlite3.exe MusicLib_Minimal_TEST_FOREIGN_KEYS.db
SQLite version 3.8.3.1 2014-02-11 14:52:19
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .databases
seq name file
--- --------------- ----------------------------------------------------------
0 main D:\MusicLib_Minimal_TEST_FOREIGN_KEYS.db
sqlite> .tables
device genre machine optiontype playCounter statement
file libType option path playlist statsource
sqlite> .schema device
CREATE TABLE "device" (
"idDevice" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"name" TEXT NOT NULL,
"source" TEXT NOT NULL,
"destination" TEXT NOT NULL,
"idPlaylist" INTEGER NOT NULL,
"idMachine" INTEGER NOT NULL,
FOREIGN KEY(idPlaylist) REFERENCES playlist(idPlaylist),
FOREIGN KEY(idMachine) REFERENCES machine(idMachine)
);
sqlite> PRAGMA foreign_keys;
0
sqlite> PRAGMA foreign_keys=ON;
sqlite> PRAGMA foreign_keys;
1
sqlite> PRAGMA foreign_key_check;
device|1|machine|0
device|1|playlist|1
sqlite> select * from device
...> ;
1|test|/|/|0|0
sqlite> select * from playlist
...> ;
sqlite> select * from machine
...> ;

Resources