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

Recommended Answers

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),
    )
Jump to Post

All 4 Replies

Re: Help in python SQL 80 80

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.

Re: Help in python SQL 80 80

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

Re: Help in python SQL 80 80

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),
    )
Re: Help in python SQL 80 80

your code works good
really thank you so much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.