How to retrieve SQL result column value using column name in Python?

后端 未结 10 1131
闹比i
闹比i 2020-11-27 14:05

Is there a way to retrieve SQL result column value using column name instead of column index in Python? I\'m using Python 3 with mySQL. The syntax I\'m looking for is pretty

10条回答
  •  青春惊慌失措
    2020-11-27 14:51

    Of course there is. In Python 2.7.2+...

    import MySQLdb as mdb
    con =  mdb.connect('localhost', 'user', 'password', 'db');
    cur = con.cursor()
    cur.execute('SELECT Foo, Bar FROM Table')
    for i in range(int(cur.numrows)):
        foo, bar = cur.fetchone()
        print 'foo = %s' % foo
        print 'bar = %s' % bar
    

提交回复
热议问题