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()
4

3 回答 3

7

你可以试试这个:

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

我第二次@Ignacio注意事项不要使用_mysql. 切换到import MySQLdb

于 2010-09-03T07:02:24.100 回答
6

您不应该导入_mysql. 以下划线开头的符号仅供私人使用。导入MySQLdb并阅读PEP 249以供使用。

于 2010-09-03T06:58:06.943 回答
0

我不确定你打算如何使用循环,但你可以这样做:

while x < result.num_rows():
    #do something for each row
    X += 1
于 2010-09-03T07:07:01.657 回答