2
import _mysql as mysql
db=mysql.connect('localhost','username','password','database')


db.query("""select * from news""")

result = db.store_result()


print result.num_rows()#two records

#how to loop? without cursor

print result.fetch_row()

3 Answers 3

7

You can try this:

while True:
    record = result.fetch_row()
    if not record: break
    print record

I second @Ignacio's note of caution against using _mysql. Switch to import MySQLdb.

Sign up to request clarification or add additional context in comments.

Comments

6

You should not be importing _mysql. Symbols that start with a single underscore are for private use. Import MySQLdb and read PEP 249 for its use.

Comments

0

I'm not sure how you plan on using the loop, but you could do something like this:

while x < result.num_rows():
    #do something for each row
    X += 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.