Re: [SQLObject] Select Single Random Record
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: Hans G. S. <geo...@sc...> - 2012-04-18 19:23:27
|
On Wed, Apr 18, 2012 at 08:46:47PM +0200, Petr Jakeš wrote:
> *Get the list of valid IDs from the remote database using the raw SQL
> command
> something like:*
>
> con = connectionForURI("firebird://yourUserName:yourPassword@serverAddress
> :3050/pat/to/database/database.file")
>
> validIDs= "SELECT ID FROM SONGS_TABLE where ......"
>
> returnedIDs = con.queryAll(validIDs)
>
> # you get the list of valid IDs, something like this: [(1,), (2,), (3,),
> (4,), (5,), (6,), (7,), (8,), (9,), (11,)]
>
> *than:*
>
> import random
> randomID = random.choice(returnedIDs)[0]
> randomSong = SongsTable.get(randomID)
Although this might work in the particular application, but
it is hardly ideal to make two queries, nor to load a long list
of IDs. Is there a way to do it with a single query?
--
:-- Hans Georg
|