Showing posts with label records. Show all posts
Showing posts with label records. Show all posts
Dec 18, 2023
Jun 9, 2022
Jun 21, 2017
SQLite: Using a table to loop
Loop
You want to create a table with lots of records for testing purposes - in SQL you could make a loop using something like this:while (@i <= 10000)
begin
insert into table1 values ( 'field1' + cast (@i as char).
'field2'+ cast (@i as char).
...
)
set @i+=1
end
go
Unfortunately in SQlite loops are not implemented - but you could use this trick instead
It will use a tIndex table, and a tRange table to generate some records in a tTarget table.
The tTarget table is for the result:
create table target (i text);
This table will receive the result as text
tRange table
create table tRange (start, finish);insert into tRange (1, 100) ;
And then afterward you can change the start and finish parameters using:
update tRange set start = 3 ;
update tRange set finish = 90 ;
tIndex table
This one is tricky because it has to contain as many records as possible - you will have to create this table via a Java program (see previous post) - by default there will be 10,000 records.Now the Loop trick
insert into tTarget(i)select ('Record number '||tIndex.id)
from tIndex join tRange
on (tIndex.id >= tRange.start and tIndex.id <= tRange.finish) ;
This will generate Record Number + start ... up to Record Number+ finish
Apr 13, 2017
SQLite: showing a random record from a table
Random record
If many occasion developer want to get a random record(s) from a table, because they are programming a quizz, or a game - it turns out there is no native instructions for that in SQLitebut you can use this workaround:
SELECT
*, random() as R
FROM Table
ORDER BY R
LIMIT 1 ;
Why it works
It insert a random number (R) for each record and then using the order by there is going to be a selection of the smaller number (R) - you can even change the LIMIT to get more than one record - let say 3SELECT *, random() as R FROM Table ORDER BY R LIMIT 3 ;
Subscribe to:
Posts (Atom)
Popular Posts
-
Assuming a table like this: create table test (id integer primary key autoincrement, name text); and values like: insert into test va...