发表用户:讨论贴
收集整理:James.Liu
相关讨论:http://www.mygis.com.cn/forum/dispbbs.asp?boardID=11&ID=513
信息原始来源:James MapInfo技术论坛

文章标题:MapXtreme for Java开发中searchByPrimaryKey的问题

wtusmchen

searchByPrimaryKey被searchByAttributes替换后,我要通过PrimaryKey查找怎么做呀?
谢谢^_^

netslihh

设置searchByAttributes中的第二个参数columnName为MapInfo_ID

wtusmchen

ArrayList columns = new ArrayList();
  columns.add("Name");
  columns.add("PY");
  columns.add("Address");
ArrayList attNames = new ArrayList();
attNames.add("MapInfo_ID");
ArrayList attOperators = new ArrayList();
attOperators.add(AttOperator.eq);
Attribute att1 = new Attribute(1);
AttTuple v1 = new AttTuple(att1);
ArrayList attvalues = new ArrayList();
attvalues.add(v1);
FeatureSet fs = null;
try{
fs = tempLayer.searchByAttributes(columns, attNames, attOperators,attvalues,null);
}catch(Exception e){e.printStackTrace();}
Feature f;
try{
        while((f = fs.getNextFeature())!=null)
       {
          System.out.println("PrimaryKey :::" + f.getPrimaryKey());
          System.out.println(f.getAttribute(0).getString());
          System.out.println(f.getAttribute(1).getString());
         System.out.println(f.getAttribute(2).getString());
      } 
}catch(Exception e){System.out.println(e.getMessage());} 

James.Liu

netslihh说的方法没错,对于TAB文件PrimaryKey就是"MapInfo_ID"这个字段,但是对于空间数据库的表可能有点不同,创建图层是自己定义的,并且添加空间图层需要显式指定PrimaryKey,最好在查找前用TableInfo.getPrimaryKeyInfo()返回表的PrimaryKey字段,然后用searchByAttributes方法查询。

wtusmchen

终于搞明白了!估计你们也是没有真正用Mapinfo_id做过,下面是mapinfo的资料:
Summary:
searchByAttributes() does not recognize MapInfo_ID as a valid column.

Question:
When attempting to use MapInfo_ID as a column for searchByAttributes, the following error may be received:

java.lang.ArrayIndexOutOfBoundsException
at com.mapinfo.dp.dat.dz.getColumnInfo(Unknown Source)
at com.mapinfo.dp.tab.dw.queryByAttributes(Unknown Source)
at com.mapinfo.dp.tab.TABDataProvider.queryByAttributes(Unknown
Source)
at com.mapinfo.mapj.Layer.searchByAttributes(Unknown Source)
at SearchByAttributesTest.main(SearchByAttributesTest.java:73)


Answer:
The TableInfo suggests that MapInfo_ID is the primary key column but the implementation doesn't support directly searching on that column like a normal column. This has been logged as incident #24128.

A workaround is to use a **real data column** as the key - for example, country name. If there is not a single unique column, use two or three or whatever it takes to make a key.

Another workaround could be to modify the .tab file in MapInfo Professional and create another column that mirrors the indexing afforded by the MapInfo_ID.