OK here's an example of counting matching records with web2py syntax.

SQL version
       select count(*) from tbbooks where id = 1 group by id;

Note!  if you don't add the GROUP BY clause,  you'll get a count
of 1 record... even if there a NO MATCHES.... probably NOT what you intended
to do.
The GROUP BY clause give you an "Empty Set" for non-matching search results

web2py version
     matches = db(db.tbbooks.id == 1).count()


On Wed, May 26, 2010 at 2:44 PM, scausten <scaus...@googlemail.com> wrote:

> I'm adding files content to my database with the following code being
> called onvalidation when the form is submitted. It really simply
> generates a 6-digit alphanumeric code as an identifier for the file:
>
> codes = db().select(db.products.code)   # Grabs all the existing codes
> from the database
> while not form.vars.code:
>    code = "".join([random.sample(string.ascii_lowercase
> +string.digits, 1)[0] for i in range(6)])  # Creates a code
>    if not code in codes:  # If its unique...
>        form.vars.code = code   # ...add it into the form vars
>
> It works fine at the moment, but I'm hoping to have several hundred
> thousand files potentially, and I'm worried that the database call to
> pull all existing codes will become a serious bottleneck. The code
> needs to be random and not sequential.
>
> Does anyone have any ideas on how I can do this more elegantly?
>

Reply via email to