private void loadTableData(
      final File dbFile, final TableViewer guiTableViewer, final String dbTableName) {
    SqlJetDb db = null;
    try {
      db = SqlJetDb.open(dbFile, true);
      final ISqlJetTable dbTable = db.getTable(dbTableName);
      final ArrayList<DataRow> data = new ArrayList<DataRow>();
      final ISqlJetTableDef tableDef = dbTable.getDefinition();
      final List<String> names = new ArrayList<String>();
      for (final ISqlJetColumnDef column : tableDef.getColumns()) {
        names.add(column.getName());
      }
      final String[] namesArray = names.toArray(new String[names.size()]);

      final Table guiTable = guiTableViewer.getTable();
      createGuiTableColumns(guiTable, namesArray);

      dbTable
          .getDataBase()
          .runReadTransaction(
              new ISqlJetTransaction() {
                @Override
                public Object run(final SqlJetDb db) throws SqlJetException {
                  final ISqlJetCursor cursor = dbTable.open();
                  try {
                    int count = 0;
                    while (!cursor.eof()) {
                      data.add(DataRow.read(cursor, count, namesArray));
                      cursor.next();
                      count++;
                    }
                  } finally {
                    cursor.close();
                  }
                  return null;
                }
              });
      // Populate data and refresh table viewer
      guiTableViewer.setInput(data);
      guiTableViewer.refresh();
    } catch (final Exception e) {
      e.printStackTrace();
    } finally {
      if (db != null) {
        try {
          db.close();
        } catch (final SqlJetException e) {
          e.printStackTrace();
        }
      }
    }
  }
Esempio n. 2
0
  private void insertDb(SqlJetDb sqlJetDb, List<?> list, String table) throws Exception {
    try {
      sqlJetDb.beginTransaction(SqlJetTransactionMode.WRITE);
      ISqlJetTable iSqlJetTable = sqlJetDb.getTable(table);
      for (int i = 0; i < list.size(); i++)
        iSqlJetTable.insertByFieldNames(this.retrieveObjectMap(list.get(i)));

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      sqlJetDb.commit();
    }
  }