List tables of sqlite database in chrome extension? - android-sqlite

I'm using sqlite manager extension in chrome to use sqlite database.I have a sqlite database.This extension works properly with select delete alter commands.but the problem is i can't list tables of database.is there any way to do this?.

You have not said what extension you are running for SO members to be able to offer definitive help.
Having said that, if you say can run SELECT queries, try:
SELECT * FROM sqlite_master WHERE type='table'
If you want only the table name, without schema details, try:
SELECT name FROM sqlite_master WHERE type='table';
Examples, including table creation
/* Create 3 table */
CREATE TABLE Your_First_Table (Id integer PRIMARY KEY, Address text);
CREATE TABLE Your_Second_Table (Id integer PRIMARY KEY, Price text);
CREATE TABLE Your_third_Table (Id integer PRIMARY KEY, Stats text);
/*Get table names and schema details */
SELECT * FROM sqlite_master WHERE type='table';
/*Get table names */
SELECT name FROM sqlite_master WHERE type='table';
Output from 1st Select:
table|Your_First_Table|Your_First_Table|2|CREATE TABLE Your_First_Table (Id integer PRIMARY KEY, Address text)
table|Your_Second_Table|Your_Second_Table|3|CREATE TABLE Your_Second_Table (Id integer PRIMARY KEY, Price text)
table|Your_third_Table|Your_third_Table|4|CREATE TABLE Your_third_Table (Id integer PRIMARY KEY, Stats text)
Output from 2nd Select:
Your_First_Table
Your_Second_Table
Your_third_Table

Related

PostgreSQL - Common autoincrement with inherited tables

I'm currently trying the inheritance system with PostgreSQL but I have a problem with the auto-increment index in my child tables.
I have three tables: "Currency", "Crypto" and "Stable"
CREATE TABLE IF NOT EXISTS public.currency
(
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name VARCHAR(30) UNIQUE NOT NULL,
symbol VARCHAR(10) UNIQUE NOT NULL,
);
CREATE TABLE IF NOT EXISTS public.stable (id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY) INHERITS (public.currency);
CREATE TABLE IF NOT EXISTS public.crypto (id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY) INHERITS (public.currency);
I insered my data like this:
INSERT INTO public.stable (name, symbol) VALUES ('Euro', '€'), ('Dollar', '$'), ('Tether', 'USDT');
INSERT INTO public.crypto (name, symbol) VALUES ('Bitcoin', 'BTC'), ('Ethereum', 'ETH'), ('Litcoin', 'LTC');
But this is my problem: I would like to have a unique identifier that increments itself through my parent table "Currency".
When I select, I have (take a look in my id: 1,2,3,1,2,3):
But, Is it possible to have something like this instead (1,2,3,4,5,6):
Is it a problem in my primary key?
Thank you
We can try to use create sequence to set row numbers for sharing between multiple tables.
define a new sequence generator
create sequence n_id;
Then we can use this sequence as below, sharing this sequence for those three tables.
create sequence n_id;
CREATE TABLE IF NOT EXISTS currency
(
id INT default nextval('n_id') PRIMARY KEY,
name VARCHAR(30) UNIQUE NOT NULL,
symbol VARCHAR(10) UNIQUE NOT NULL
);
CREATE TABLE IF NOT EXISTS stable (id INT default nextval('n_id') PRIMARY KEY) INHERITS (currency);
CREATE TABLE IF NOT EXISTS crypto (id INT default nextval('n_id') PRIMARY KEY) INHERITS (currency);
sqlfiddle

postreqsql: inserting into 2 tables linked by foreign key so serial id is shared

Hello I'm trying to learn the very basics of Postgresql
How can I insert data (name, lastname) into both tables at the same time so that the serial id connects it(is the same). And then delete one entry in both tables that are connected.
My tables I'm trying to figure that out with:
CREATE TABLE user
(
"userid" serial NOT NULL,
name character varying(30),
PRIMARY KEY ("userid")
);
CREATE TABLE public.passwords
(
"userid" integer NOT NULL,
lastname character varying(30),
CONSTRAINT "user_userid" FOREIGN KEY ("userid")
REFERENCES public.user ("userid") MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE
NOT VALID
);
You can to insert in both tables sequentially with one query, using the serial generated by the first insert in the second insert as follows:
with u as (insert into users (name) values ('foo') returning userid)
insert into passwords (userid, lastname) select userid, 'bar' from u;

jsonb_populate_record / jsonb_populate_recordset should return a table

currently I try to make a history table based on postgresql jsonb, currently as a example I have two table's:
CREATE TABLE data (id BIGSERIAL PRIMARY KEY, price NUMERIC(10,4) NOT NULL, article TEXT NOT NULL, quantity BIGINT NOT NULL, lose BIGINT NOT NULL, username TEXT NOT NULL);
CREATE TABLE data_history (id BIGSERIAL PRIMARY KEY, data JSONB NOT NULL, username TEXT NOT NULL);
The history table act's a simple history (the username there could be avoided).
I populate the data of the history with a trigger:
CREATE OR REPLACE FUNCTION insert_history() RETURNS TRIGGER AS $$
BEGIN
INSERT INTO data_history (data, username) VALUES (row_to_json(NEW.*), NEW.username);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
Now I try to populate the history back to the data table:
SELECT jsonb_populate_record(NULL::data, data) FROM data_history;
However the result will now be a tuple and not a table:
jsonb_populate_record
-------------------------------------
(1,45.4500,0A45477,100,1,c.schmitt)
(2,5.4500,0A45477,100,1,c.schmitt)
(2 rows)
Is there any way to get the data back as the table data back? I know there is jsonb_populate_recordset, too, however it doesn't accept a query?!
jsonb_populate_record() returns a row-type (or record-type), so if you use it in the SELECT cluase, you'll get a single column, which is a row-type.
To avoid this, use it in the FROM clause instead (with an implicit LATERAL JOIN):
SELECT r.*
FROM data_history,
jsonb_populate_record(NULL::data, data) r
Technically, the statement below could work too
-- DO NOT use, just for illustration
SELECT jsonb_populate_record(NULL::data, data).*
FROM data_history
but it will call jsonb_populate_record() for each column in data (as a result of an engine limitation).

How to insert a primary/foreign key row in PostgreSQL

I'm populating a database in PostgreSQL for a Newspaper Online. Now my doubt lies on how to insert a value into a table which only attribute is both a primary and a foreign key.
In this context, the admin is the first person to ever register an account. So idAdmin = idA = 1:
CREATE TABLE AUTENTICADO (
idA serial NOT NULL ,
login VARCHAR(45) NOT NULL,
password VARCHAR(45) NOT NULL,
email VARCHAR(40) NOT NULL,
PRIMARY KEY (idA) );
CREATE TABLE ADMIN (
idAdmin INT NOT NULL REFERENCES AUTENTICADO (idA),
PRIMARY KEY (idAdmin) );
It would be logical to insert values into 'ADMIN' as I tried below, although it is obviously not possible considering 'idAdmin' is a primary key (and a foreign key).
INSERT INTO AUTENTICADO VALUES ('john','adadfsfsdfs', 'john#random.com')
INSERT INTO ADMIN VALUES (1)
Is there a way to register that the first user to create an account (idA = 1) is the admin (idAdmin = idA = 1) ?
although it is obviously not possible considering 'idAdmin' is a
primary key (and a foreign key).
So what?
If you fix the first query to list the columns and use a returning clause to get the auto-generated value for the SERIAL ID, it just works:
INSERT INTO AUTENTICADO(login,password,email)
VALUES ('john','adadfsfsdfs', 'john#random.com')
returning idA;
Result:
ida
-----
1
(1 row)
Second query:
insert into admin values(1);
select * from admin;
Result:
idadmin
---------
1

insert into and select

how would the query on:
Update the field total_horas with the hours worked on each project
I have:
insert into proyecto(total_horas)
select trabaja.nhoras
from trabaja;
But it's trying to insert in the first firld of "proyecto" instead on the field "total_horas"
my table:
CREATE TABLE proyecto (
cdpro CHAR(3) NOT NULL PRIMARY KEY,
nombre VARCHAR(30),
coddep CHAR(2),
FOREIGN KEY (coddep)
REFERENCES departamento(cddep)
ON DELETE CASCADE
);
also altered with: alter table proyecto ADD total_horas char ;
You have to put a where condition in select statement.And please elaborate you question. trabaja.nhoras is the column name and you are selecting it from table trabaja
Example:
INSERT INTO proyecto
(total_horas)
SELECT trabaja.nhoras
FROM trabaja
WHERE 'condition' = 'some condition';