Itachi Sama
Itachi Sama

Reputation: 926

how do i fetch sqlite column based on its len and limting 10 using python

Trying to list sqlite3 value from highest to lowest limited it to 10 using python, here's my current code

connection   = sqlite3.connect('mydb.db')
database     = connection.cursor()
all_user     = str(database.execute("SELECT logtext from logs order by logtext limit 10 ")

I can't figure the logic of using len(logtext) and how to actually list from highest to lowest limiting them by 10.

Upvotes: 0

Views: 66

Answers (1)

Joe P
Joe P

Reputation: 485

Try

connection = sqlite3.connect('mydb.db')
cursor = connection.cursor()
cursor.execute("SELECT logtext from logs order by length(logtext) desc limit 10")
results = cursor.fetchall()

See also https://stackoverflow.com/a/3606923/960709

Upvotes: 1

Related Questions