Date: 2004-05-05T09:03:30 Editor: 194.175.229.106 <> Wiki: DB Torque Wiki Page: FrequentlyAskedQuestions URL: http://wiki.apache.org/db-torque/FrequentlyAskedQuestions
no comment Change Log: ------------------------------------------------------------------------------ @@ -247,3 +247,26 @@ While coding your own transactions, make sure that the Database connection is released at the end. This is ensured in the above code because either Transaction.commit() or Transaction.rollback() are called, both of which release the database connection. Note that Transaction.safeRollback() does not release the database connection, so replacing Transaction.rollback() by transaction.safeRollback() in the above code would lead to the leakage of database connections if the transaction fails. -- Thomas Fischer + +== How can I count datasets in a table ? == + +Suppose one wants to execute a SQL-statement like "select count(*) from employee where name='james'". + +'''Answer:''' This query can be executed using the following code (assuming the employee Table has at least two Columns: "NAME" and "ID", where ID is the primary key, and the Peer class to the employee Table is EmployeePeer (sorry the link does not work but I don't know how to get rid of it)): +{{{ +int numberOfRecords; +{ + org.apache.torque.util.Criteria criteria = new org.apache.torque.util.Criteria(); + criteria.add(EmployeePeer.NAME, "james"); + criteria.addSelectColumn("COUNT(" + EmployeePeer.ID + ")"); + java.util.List result = EmployeePeer.doSelectVillageRecords(criteria); + com.workingdogs.village.Record record = (com.workingdogs.village.Record) result.get(0); + numberOfRecords = record.getValue(1).asInt(); +} +}}} + +I don't know which columns should be used in addSelectColumn (and if it makes a difference which one is used), but I presume using the primary key is fine. Note that addSelectColumn("count(*)") does not work. + +The answer was put together from articles by Scott Eade and Tulsi Das on the Torque Users Mailing List. + +-- Thomas Fischer --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]