2016-10-10 190 views
1

我有一串字符串,其长度变化很大:从30到282420个字符。我想使用jdbc将每个字符串存储到postgres数据库中。存储到PostgreSQL时是否存在最大长度TEXT

 String query = "INSERT INTO " + tableName + 
         " (cbsID, ownerID, naam, soortRegio, jaar, centroidLat, centroidLon, borders) " + 
         " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; 
    PreparedStatement ps = connection.prepareStatement(query); 
    for (Marker marker: gemeenteMarkers) 
    { 
     Area gemeente = (Area) marker.getProperty("area"); 
     String sloc = ""; 

     // ... do some computations 
     // Compute sloc, a string that may contain over 250.000 characters 
     ps.setInt (1, gemeente.getAreaId()); 
     ps.setInt (2, gemeente.getParentId()); 
     ps.setString (3, gemeente.getAreaName()); 
     ps.setString (4, "GM"); 
     ps.setInt (5, 2014); 
     ps.setFloat (6, gemeente.getCentroid().getLat()); 
     ps.setFloat (7, gemeente.getCentroid().getLon()); 
     ps.setString (8, slocs); 
     System.out.printf ("%s - %d locations, len = %d\n", gemeente.getAreaName(), locs.size(), slocs.length()); 

     ps.addBatch(); 
    } // for 
    int [] affected = ps.executeBatch(); 

的信息存储在数据库中的所有的权利,除了slocs,其中包含字符串提到并存储在列'边界。请参阅下面的pdAdmin屏幕截图。 Screenshot of the table. Notice the absence of a lot of elements in the boder column

边界被定义为TEXT。正如您在大多数情况下所看到的,它不会被存储,除非它很短,通常小于7000个字符。该代码包含print语句和输出的一部分如下所示:

Appingedam - 1649 locations, len = 32980 
Bedum - 1210 locations, len = 24200 
Bellingwedde - 1500 locations, len = 30000 
Ten Boer - 1186 locations, len = 23720 
Delfzijl - 16 locations, len = 320 
Groningen - 2662 locations, len = 53240 
Grootegast - 4843 locations, len = 96860 
Haren - 1940 locations, len = 38800 
Hoogezand-Sappemeer - 1481 locations, len = 29620 
Leek - 2575 locations, len = 51500 
Loppersum - 2991 locations, len = 59820 
Marum - 1936 locations, len = 38720 
Almere - 213 locations, len = 4260 
Stadskanaal - 2701 locations, len = 54020 
Slochteren - 1555 locations, len = 31100 
Veendam - 1098 locations, len = 21960 
Vlagtwedde - 2621 locations, len = 52420 
Zeewolde - 25 locations, len = 500 
Winsum - 2992 locations, len = 59840 
Zuidhorn - 5282 locations, len = 105640 
Dongeradeel - 256 locations, len = 5120 
Achtkarspelen - 4644 locations, len = 92880 
Ameland - 158 locations, len = 3160 
het Bildt - 2337 locations, len = 46740 
Franekeradeel - 50 locations, len = 1000 
Harlingen - 50 locations, len = 1000 
Heerenveen - 5195 locations, len = 103900 

在Postgres的手册,我发现

如果您希望长字符串存储,没有具体的上限,使用 文字或字符变化而没有长度说明符,而不是构成任意长度限制的 。

似乎有一些长度限制,虽然只有短文本显示在pgAdmin屏幕上。有谁知道我做错了什么?

编辑1

对不起,暂时不明朗。我添加了代码示例,并且包含程序输出和pgAdmin输出以显示表中的数据。

编辑2

我已经加入一个executeBatch于该示例。它出现在原始代码中,输出不会改变。

+0

“任何想法,我做错了什么”---你没有解释这个问题,所以我们不知道你做错了什么。 – zerkms

+0

是什么让你觉得有一个限制?爆炸了吗?您的输入数据在某处意外被截断了吗?你能发布代码来重现你的问题吗? – Bohemian

+0

限制为1GB:https://wiki.postgresql.org/wiki/FAQ#What_is_the_maximum_size_for_a_row.2C_a_table.2C_and_a_database.3F –

回答

0

关于缺乏例子,有一些正当的评论。在玩例子时,我偶然发现了pgAdmin的一些显着的行为:它没有显示出现的信息。只需从原始问题中显示的pgAdmin数据编辑器的输出中选择一些行并将其粘贴到编辑器中即可。然后确实显示在pgAdmin的数据显示中不可见的信息,见下文。

3;0;"Appingedam";"GM";2014;53.3172;6.84957;"53.345131 6.885408 53.344925 6.885422 ... 
5;0;"Bedum";"GM";2014;53.2949;6.59717;"53.255638 6.587573 53.255646 6.587514 53.25... 
7;0;"Bellingwedde";"GM";2014;53.0986;7.12118;"53.097076 7.038233 53.097099 7.038254... 
9;0;"Ten Boer";"GM";2014;53.2788;6.69145;"53.312183 6.740470 53.312176 6.740486 53... 
10;0;"Delfzijl";"GM";2014;53.3227;7.09945;"53.322491 7.100943 53.322414 7.100940 5... 
14;0;"Groningen";"GM";2014;53.2222;6.5634;"53.198223 6.515755 53.198238 6.515728 5... 
15;0;"Grootegast";"GM";2014;53.2086;6.27322;"53.203838 6.318381 53.203819 6.318180 ... 
17;0;"Haren";"GM";2014;53.1545;6.63106;"53.165165 6.560897 53.165207 6.560884 53.1... 
18;0;"Hoogezand-Sappemeer";"GM";2014;53.1473;6.7468;"53.116516 6.743022 53.116371 6... 
22;0;"Leek";"GM";2014;53.1633;6.36926;"53.211250 6.362241 53.211262 6.362327 53.21... 

如此看来,pgAdmin的行为就像你不看是你没有得到。 Java,jdbc和Postgres按预期行事,pgAdmin没有。

+0

http://stackoverflow.com/q/31828182/330315 –

+0

不eactly:我不明白“...”,这是可以接受的。我什么都得不到,导致错误的结论。提到的链接已经死了。 – Arnold

+0

https://www.pgadmin.org/docs/1.22/options-query_tool.html –

1

根据Postgres character type documentation,textvariable unlimited length
进一步回落:

... PostgreSQL支持text类型,其中存储任何长度的字符串。

然而

按照Postgres Wiki,有1GB的单个列值的硬性限制,所以实际限制为“无限制”长度text值由限制为1GB架构。

+1

限制为1GB:https://wiki.postgresql.org/wiki/FAQ#What_is_the_maximum_size_for_a_row.2C_a_table.2C_and_a_database.3F –

相关问题