How can I insert 1000 times in one statement? with SQLITE?

-edit-, to make it more understandable, I just used the cmd line (in fact, in this case) and wanted to do a quick test with ram and didn't feel like you were making a full-blown prj for a quick test test.

I want to populate this table with 10,000,000 values, but first I want only 1,000.

I tried this in the sqlite database browser, but 3 is not inserted unless I reset everything after it. But more importantly, I don't know how to make num from 1 to 1000.

create table if not exists test1(id integer primary key, val integer);
insert into test1(val) select '3' as num where num between 1 and 1000
+3
source share
4 answers

OK, here is a way to do it in pure SQL ...

create table if not exists test1(id integer primary key, val integer);

create trigger test1_ins_trigger after insert on test1
  when new.val < 1000 begin
    insert into test1(val) values(new.val + 1);
  end;

pragma recursive_triggers = 1;

insert into test1(val) values(1);
+10
source
CREATE TEMP TABLE Bits (Bit INTEGER PRIMARY KEY);
INSERT INTO Bits VALUES (0);
INSERT INTO Bits VALUES (1);

CREATE TEMP TABLE Nums AS SELECT
     b9.Bit * 512 + b8.Bit * 256 + b7.Bit * 128 + b6.Bit * 64 + b5.Bit * 32 +
     b4.Bit * 16 + b3.Bit * 8 + b2.Bit * 4 + b1.Bit * 2 + b0.Bit
     AS Num
FROM Bits b9, Bits b8, Bits b7, Bits b6, Bits b5,
     Bits b4, Bits b3, Bits b2, Bits b1, Bits b0;

CREATE TABLE Test1 (ID INTEGER PRIMARY KEY, Val INTEGER);
INSERT INTO Test1 SELECT Num, 3 FROM Nums WHERE Num BETWEEN 1 AND 1000;
+6
source

- SQLite? , - (php/perl/pick your poison) INSERT .

For a unique SQLite specific solution, you can use virtual tables and create a module for implementing virtual table 1..1000.

0
source

If you can use Python:

import sqlite3
conn = sqlite3.connect(":memory:")
c = conn.cursor()
c.execute("create table test1 (id integer primary key, val integer);")
L = zip(range(1,1001))
c.executemany("insert into test1 (val) values (?);", L)
c.execute("select min(val), max(val) from test1;").fetchone()
#(1, 1000)
0
source

Source: https://habr.com/ru/post/1746621/


All Articles