jackcess delete row and set autoincrement column

不打扰是莪最后的温柔 提交于 2019-12-11 10:43:53

问题


  1. How delete row from table with help jackcess? I try so, but it's bad:

        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. How set column "id" attribute to autoincrement?

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


回答1:


If your id column is indexed, you can use an IndexCursor to quickly find columns:

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

If your id column is not indexed, you can use a normal cursor, which is more convenient but effectively no faster than your current code (just does a table scan):

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

And your own answer indicates you already figured out how to make a column auto increment.




回答2:


For set autoincrement to column:

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);


来源:https://stackoverflow.com/questions/9343470/jackcess-delete-row-and-set-autoincrement-column

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