How to Iterate through cur.fetchall() in Python

前端 未结 5 919
刺人心
刺人心 2021-01-02 18:18

I am working on database connectivity in Python 3.4. There are two columns in my database.

Below is the query which gives me all the data from two columns in shown f

5条回答
  •  失恋的感觉
    2021-01-02 18:41

    As the output you given [('F:\\test1.py', '12345abc'), ('F:\\test2.py', 'avcr123')]

    for i, row in data:
        print(i)  # i is column1's value
        print(row)# row is column's value
    

    So you don't need row[i] or row[j], that was wrong, in that each step of that iteration

    for i, row in data 
    

    is the same as i, row = ('abc', 'def') it set abc to variable i and 'def' to row

    BTW ,I don't know what database you use, if you use Mysql and python driverMySQL Connector, you can checkout this guide to fetch mysql result as dictionary you can get a dict in iteration, and the keys is your table fields' name. I think this method is convenient more.

提交回复
热议问题