报错 cursor() takes 1 positional argument but 2 were

Sam

def selectRecord(request):

    #cursor = conn.cursor()  

    cursor = conn.cursor(pymysql.cursors.DictCursor)

    cursor.execute('select * from book ', [])

    books = cursor.fetchall()

    conn.close()

    return HttpResponse(books)


这段代码用conn.cursor()  可以正常执行

用 cursor = conn.cursor(pymysql.cursors.DictCursor)

报错 cursor() takes 1 positional argument but 2 were given

这是为什么?


  • Sam
  • Sam     2020-06-19 18:25:491楼

    补充代码:

    这是前面的代码

    from django.http import HttpResponse
    from django.db import connection as conn
    import pymysql

    发现一个问题用from django.db import connection as conn 导入的conn

    conn.cursor()不能修改游标类型会报错 cursor() takes 1 positional argument but 2 were given


    如果是

    conn = pymysql.connect(  
       host='localhost',
       user='***',  # 数据库登录用户名
       password='***',  # 登录用户密码
       db='***'
    )

    这样获得的conn就可以修改游标类型如:

     cursor = conn.cursor(pymysql.cursors.DictCursor)


    一直没找到原因,盘高人指点

    +添加回复