【问题标题】:how to using MySQLdb SELECT with for or while loop如何在 for 或 while 循环中使用 MySQLdb SELECT
【发布时间】:2011-04-07 16:58:33
【问题描述】:
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()

【问题讨论】:

    标签: python mysql mysql-python


    【解决方案1】:

    你可以试试这个:

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

    我反对 @Ignacionote of caution 反对使用 _mysql。切换到import MySQLdb

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 2021-07-27
          • 1970-01-01
          • 2013-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-01
          • 2013-11-07
          • 1970-01-01
          相关资源
          最近更新 更多