I have the following sqlite db:
BEGIN TRANSACTION;
CREATE TABLE `table1` (
`Field1` TEXT
);
INSERT INTO `table1` VALUES ('testing');
INSERT INTO `table1` VALUES ('123');
INSERT INTO `table1` VALUES ('87654');
COMMIT;
This select returns the correct result:
select * from table1 where Field1 like '%e%';
However this one returns nothing?
select * from table1 where Field1 like '%2%';
Even Stranger in DB Browser for SQLite:
select * from table1 where CAST(Field1 AS Text) LIKE '%2%'
Returns:
1 Rows returned from: select * from table1 where CAST(Field1 AS Text) LIKE '2%' (took %3ms)
Maybe a bug? Drops the first %
A bug in DB browser. I raised an issue and its now been fixed in the nightlies.
Related
I've looked at the other postings but I haven't seen a resolution for this.
CREATE TABLE newtable AS (SELECT * FROM yourtable) with no data ;
Then
with cte as (
select top 10* from yourtable
)
insert into newtable
select * from cte
select * from newtable
I get an error saying it expects a select and not an insert.
How would I accomplish inserting the cte into a table?
I have 2 tables with identical structure I want to update one table using data from the other, matching on primary key. SQLite has a with (CTE) statement but the following doesn't work (sqlite3 v. 3.29.0):
sqlite> select * from main;
1|A
2|B
4|D
5|E
6|F
sqlite> select * from temp;
1|aa
2|bb
3|cc
4|dd
5|ee
sqlite> with mapping as (select main.ID, temp.Desc from main join temp on temp.ID=main.ID) update main set Desc=mapping.Desc where main.ID=mapping.ID;
Error: no such column: mapping.Desc
I've tried using "select main.ID as ID, temp.Desc as Desc", but get the same error message.
To update your main table from your cte, use a subquery, since sqlite doesn't support update from
with mapping as
(select main.ID, temp.Desc
from main
join temp on temp.ID=main.ID)
update main set Desc=
(select Desc from mapping where ID = main.ID limit 1);
see dbfiddle
here is my query
select *
from table
where id in ( select string_agg(CAST(id as varchar), '","') FROM table)
string_agg() is completely useless and unnecessary for that:
select *
from table_one
where id in (select id FROM other_table)
I assume you are doing that for two different tables, otherwise that would be a very expensive way of writing: select * from table where id is not null
I'm trying to insert a parent and child at the same time.
My idea is to insert the parent, get the id using SELECT last_insert_rowid() AS [Id] and use this id to insert the child
I can get each part of this working independently but not as a whole. This is what I currently have:
INSERT INTO ParentTable (Col1)
VALUES( 'test')
SELECT last_insert_rowid() AS [Id]
The above works - so far so good. Now I want to use the result of this in the child insert. This is what I have:
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES( 1, 2, SELECT Id FROM (
INSERT INTO ParentTable (Col1)
VALUES( 'test')
SELECT last_insert_rowid() AS [Id]
);
I get this error:
near "SELECT": syntax error:
Can anyone point me in the right direction?
You can't use INSERT in SELECT statement. You should first insert and then use last inserted id:
INSERT INTO ParentTable (Col1) VALUES( 'test');
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES(1,2, (SELECT last_insert_rowid()));
Since you want to insert many records with parent ID, here is a workaround:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE IF NOT EXISTS temp(id integer);
DELETE FROM temp;
INSERT INTO ParentTable (Col1) VALUES( 'test');
INSERT INTO temp SELECT last_insert_rowid();
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES(1,2, (SELECT id FROM temp LIMIT 1));
.............
COMMIT;
DROP TABLE temp;
Or you can create a permanent table to this effect.
That SQLite.Net PCL driver assumes that you use the ORM: inserting an object will automatically read back and assign the autoincremented ID value.
If you're using raw SQL, you have to manage the last_insert_rowid() calls yourself.
Your idea is correct, but you have to do everything in separate SQL statements:
BEGIN; -- better use RunInTransaction()
INSERT INTO Parent ...;
SELECT last_insert_rowid(); --> store in a variable in your program
INSERT INTO Child ...;
...
END;
(SQLite is an embedded database and has no client/server communication overhead; there is no reason to try to squeeze everything into a single statement.)
If I execute the following script in sqlite 3.8.8.3
PRAGMA foreign_keys=on;
create table A (
k integer primary key not null
);
create table B (
value text primary key,
k integer references A(k) on delete cascade
);
create table C (
k integer primary key,
value text
);
create trigger C_A before insert on C
begin
insert or ignore into A values(NEW.k);
end;
insert into A values (5);
select * from A;
insert into B values ('asdf', 5);
select * from B;
insert or ignore into A values(5); --just calling this works right
select * from B;
insert or ignore into C values (5, 'qwer'); --this works too
select * from B;
insert or replace into C values (5, 'qwer'); --this doesn't work
select * from B; --doesn't print anything
I get the output
5
asdf|5
asdf|5
asdf|5
when I'm expecting the output
5
asdf|5
asdf|5
asdf|5
asdf|5
It seems like the "or replace" gets propagated into the trigger, replacing the row in A, and deleting the row in B.
Is this expected behavior? Am I misunderstanding what this should do?
This is documented:
An ON CONFLICT clause may be specified as part of an UPDATE or INSERT action within the body of the trigger. However if an ON CONFLICT clause is specified as part of the statement causing the trigger to fire, then conflict handling policy of the outer statement is used instead.