Sort a table in Jackcess

纵饮孤独 提交于 2019-12-13 19:26:40

问题


I work with an MS-Access table in Java using Jackcess:

Database mdb = Database.open(new File(myPath));
Table myTable = mdb.getTable("TableName"); 

Is there a way to get the table sorted/ordered by one or more column(s)? Couldn't find anything in the docs.

Thanks for any hint.


回答1:


If you iterate through the Table rows using a Cursor which is backed by an Index, you will get the rows ordered by the relevant Index.

This is an example (using the 1.x API) which iterates the table based on the order of the primary key:

for(Map<String,Object> row : Cursor.createIndexCursor(table, table.getPrimaryKeyIndex())) {
  // do something with row here...
}



回答2:


I was having this same issue here, but it helped.

For the folks that are using the new version of Jackcess (v: 2.1.2) here is the answer:

for (Row row : CursorBuilder.createCursor(table.getIndex("IndexToBeSorted"))){

           //Your awesome code with the row here      

}

Thanks!



来源:https://stackoverflow.com/questions/18787142/sort-a-table-in-jackcess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!