jackcess删除行并设置autoincrement列

时间:2012-02-18 17:35:08

标签: java jackcess

  1. 如何使用help jackcess从表中删除行? 我试试,但这很糟糕:

        Table ptabl = db.getTable("person");
        int pcount = ptabl.getRowCount();
        for (int i = 0; i < pcount; i++) {
            Map<String, Object> row2 = ptabl.getNextRow();
            if (row2.get("id") == Integer.valueOf(1)) {
                ptabl.deleteCurrentRow();
            }
        }
    
  2. 如何将列“id”属性设置为自动增量?

    Table newTable = new TableBuilder("diagnosis"). addColumn(new ColumnBuilder("id") .setSQLType(Types.INTEGER) .toColumn()) .addColumn(new ColumnBuilder("name") .setSQLType(Types.VARCHAR) .toColumn()).toTable(db);

2 个答案:

答案 0 :(得分:3)

如果您的id列已编入索引,则可以使用IndexCursor快速查找列:

IndexCursor cursor = new CursorBuilder(ptabl).setIndexByColumnNames("id").toIndexCursor();
if(cursor.findFirstRowByEntry(1)) {
  cursor.deleteCurrentRow();
}

如果您的id列未编入索引,则可以使用普通光标,这样更方便,但实际上并不比当前代码快(仅进行表扫描):

Cursor cursor = new CursorBuilder(ptab1).toCursor();
Column idCol = ptab1.getColumn("id");
if(cursor.findFirstRow(idCol, 1)) {
  cursor.deleteCurrentRow();
}

你自己的答案表明你已经想出了如何使列自动增量。

答案 1 :(得分:1)

用于设置自动增量到列:

Table newTable = new TableBuilder("diagnosis").addColumn(new ColumnBuilder("id").setAutoNumber(true).setSQLType(Types.INTEGER).toColumn()).addColumn(new ColumnBuilder("name").setSQLType(Types.VARCHAR).toColumn()).toTable(db);