2017-10-09 52 views
0

我在SQLite Studio中有一個名爲「LOCATIONS」的表。該表具有下述列:使用派生自自定義腳本的值填充大型SQLite數據庫

  • LOCATION_ID(PK)
  • location_x
  • location_y

我希望與在一個2000x1000矩陣的ID和每個像素的位置來填充該數據庫( 1,0,0; ​​2,0,1; 3,0,2; ... 2000000,1999,999)。

首先我跑的Java以下內容並保存輸出到.txt:

public class matrix { 
    public static void main(String[] args) { 
    int c = 1; 
    for (int x = 0; x < 2000; x++) { 
     for (int y = 0; y < 1000; y++) { 
     System.out.print("('" +c+ "','" +x+ "','" +y+ "')\n"); 
     c++; 
     } 
    } 
    } 
} 

然後我(原諒我缺乏編程知識在這裏)試圖直接複製2M線路成一個INSERT語句中SQLStudio的SQL編輯器,但是我的電腦無法處理它並且每次都崩潰程序。

什麼是我的問題的最佳解決方案?

+0

忘記的Java和SQLite的命令行只使用SQL。 –

回答

2

我可以建議你做location_id (pk)作爲INTEGER PRIMARY KEY AUTOINCREMENT場然後,而不是生成行的所有值使用CTE這樣的:

with recursive x(i) as(
    select 1 
    union all 
    select i+1 
    from x 
    where i<2000 
), y(i) as(
    select 1 
    union all 
    select i+1 
    from y 
    where i<1000 
), t as (
    select x.i x, y.i y 
    from x 
    cross join y 
) 
insert into LOCATIONS (location_id, location_x,location_y) 
select null, x, y 
from t 
order by x, y; 

SQL Fiddle Demo

+0

使用計算出的ID更好地思考以確保正確的ID,但此解決方案要快得多。 –

相關問題