how to get the ordered query in sqlite? - 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;

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>

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 select from sqlite table and dump to .sql file?

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

SQLite time insert query

I want to insert time value to SQLite. I searched about functions, modifiers, timestrings but I could not achieve to my aim. When I write my query, this does not record the '.323', only records '08:25:01'. I want to record '08:25:01.323'. My query is:
insert into table_name (column_name) values (time('08:25:01.323'))
I'm waiting for your help..
Store the time as a string and only parse it whe necessary.
sqlite> create table t (t text);
sqlite> insert into t values ('08:25:01.323');
sqlite> select * from t;
08:25:01.323
sqlite> select t, time(t) from t;
08:25:01.323|08:25:01

SQLite Store strings that start with zero e.g. "001"

Which data type do I choose in SQLite to store a string such as "001" in the database.
I've tried using Text but when I go to type in the string 001, it drops the 00 and only puts a 1.
I've even tried using "blob" data type but still no luck. They all drop the zeros before the digit.
Works ok for me:
sqlite> create table sotest
...> (
...> col1 varchar(20)
...> );
sqlite>
sqlite> select * from sotest;
sqlite>
sqlite> insert into sotest values ('001');
sqlite> select * from sotest;
001
sqlite>
Maybe you weren't quoting your strings? For example:
sqlite> insert into sotest values ('001');
sqlite> select * from sotest;
001
sqlite> insert into sotest values (001);
sqlite> select * from sotest;
001
1
sqlite>
Column type should set to varchar, not string, if type is string zero will be dropped automatically.

Resources