Selecting a random row

Not sure if this is the right group to post this question, but I didnt see anything that seemed appropriate. Is there a way I can select a random row from a table? I’m not sure if SeaTables SQL will support something like that. Thanks for any help or advice…

This can only be achieved with JS or Python in SeaTable.
Try this…

import random
from seatable_api import Base, context

# Initialize the SeaTable connection
server_url = "https://cloud.seatable.io"
api_token = "your_api_token_here"
base = Base(api_token, server_url)
base.auth()

# Specify the table name
table_name = "Your_Table_Name"

# Get all rows from the table
rows = base.list_rows(table_name)

# Check if the table has any rows
if rows:
    # Select a random row
    random_row = random.choice(rows)
    print("Random row selected:")
    print(random_row)
else:
    print("The table is empty.")

Please give feedback.

Just one comment: list_rows return 1000 rows at most. If your table is longer than that, you should use the query endpoint.

I am using the API to read/write my table through VB.Net. It looks like my table has an index column, numbered sequentially, even though I didnt specifically create it when I created my table. Can I use this column? So basically I would generate a random number in my code & then retrieve the row at that index. Is that do-able?

The _id column is created by SeaTable and managed by SeaTable.

First, what is wrong with the suggestion provided above.
Second, I don’t see how your suggestion is going to work: Your generated random number will most likely have no correspondence in the table.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.