pymysql - python3.5操作数据库中fetchall的问题
ringa_lee
ringa_lee 2017-04-18 09:43:49
0
2
445

import pymysql.cursors
import networkx as nx

Connect to the database

connection = pymysql.connect(host='localhost',

                         port=3306,
                         user='root',
                         password='',
                         db='shortest',
                         charset='utf8mb4',
                         cursorclass=pymysql.cursors.DictCursor)

执行sql语句

try:

    cursor = connection.cursor()
    # 执行sql语句,进行查询
    sql = 'SELECT author1,author2,year FROM shortest WHERE id < 200'
    cursor.execute(sql)
    # 获取查询结果
    result = cursor.fetchall()
    for row in result:
        author1 = row[1]
        author2 = row[2]
        year = row[3]
        # 打印结果
        print
        "auther1=%s,author2=%s,year=%s" % \
        (myauther1, myauther2, myyear)
    # 没有设置默认自动提交,需要主动提交,以保存所执行的语句
    connection.commit()

finally:

connection.close();

这个代码我感觉没有问题,但是编译器却报错!!!
Traceback (most recent call last):
File "E:/Python Work/beta1/test.py", line 21, in <module>

author1 = row[1]

KeyError: 1

希望高手可以解释指点一下,谢谢啦!

ringa_lee
ringa_lee

ringa_lee

모든 응답(2)
迷茫

결과는 Python에서 dict['Name'] 형식으로 사용해야 하는 사전 구조여야 합니다. 자세한 내용은 바이두 사전을 참고하세요.

迷茫

먼저 결과를 인쇄해 보세요

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!