0

Hello,
i want ask somthing about mysql in multi pages i hope someone help me
and sorry about my English

this code works good but there problem if i used it with this tybe

sql = "SELECT * FROM emaillogs ORDER BY id DESC"
                cursor.execute(sql)
                results = cursor.fetchall()
                if page <= "1":
                    for row in results[0:30]:
                        print row

                else: 
                    NUM2 = int(page) * 30
                    NUM1 = NUM2 - 30
                    for row in results[int(NUM1):int(NUM2)]:
                        print row

and the page link will be domain.com/file.py?do=emaillogs&page=1

this code if big data in table will cause load and long time to process because the sql code will get all rows in the table in memory and python code "for row in results[0:30]:" will show after that 30 only in the page at browser
i want code get data limit direct from mysql code

2
Contributors
4
Replies
61
Views
3 Years
Discussion Span
Last Post by zoro007
Featured Replies
  • Try sql = ("SELECT * FROM `emaillogs`" " WHERE `id` BETWEEN {i1:d} AND {i2:d}" " ORDER BY `id` DESC").format( i1 = int(limit1), # type checking with conversion i2 = int(limit2), ) Read More

0

It seems that you must use the LIMIT keyword in the SELECT statement, as described in the mysql documentation. For example

SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

See also this. I dont know how it will interact with your ORDER BY clause.

Edited by Gribouillis

0

yes i know this but how can do this with Variables ?
I mean as you said and also this works

"SELECT * FROM emaillogs WHERE id BETWEEN 10 AND 15 ORDER BY id DESC"

but i want use Variables like this
link:
domain.com/file.py?do=emaillogs&limit1=5&limit2=10

"SELECT * FROM emaillogs WHERE id BETWEEN 10 AND 15 ORDER BY id DESC"

How recoup BETWEEN 10 AND 15 in Variables ?

when i use this appear to me error : "SELECT * FROM emaillogs WHERE id BETWEEN '%s' AND '%s' ORDER BY id DESC" %(limit1,limit2)

i hope you understand me

1

Try

sql = ("SELECT * FROM `emaillogs`"
    " WHERE `id` BETWEEN {i1:d} AND {i2:d}"
    " ORDER BY `id` DESC").format(
        i1 = int(limit1), # type checking with conversion
        i2 = int(limit2),
    )

Edited by Gribouillis

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.