zoukankan      html  css  js  c++  java
  • Python3链接数据库报错:Connection.__init__() takes 1 positional argument but 5 positional arguments (and 1 keywordonly argument) were given

    一、现象

    Python3链接数据库报错:Connection.__init__() takes 1 positional argument but 5 positional arguments (and 1 keyword-only argument) were given

    二、解决

     把以下红色位置:

    import pymysql
    # 打开数据库连接
    try:
    db = pymysql.connect("localhost", "您的用户名", "您的密码", "数据库名称", charset='utf8' )
    print('数据库连接成功')
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()

    # 使用execute方法执行SQL语句
    cursor.execute("SELECT VERSION()")

    # 使用 fetchone() 方法获取一条数据
    data = cursor.fetchone()

    print(data)

    # 关闭数据库连接
    db.close()
    except pymysql.Error as e:
    print('数据库连接失败:' + str(e))

    更新后即:

    import pymysql
    # 打开数据库连接
    try:
    db = pymysql.connect(host="localhost", user="您的用户名", password="您的密码", database="数据库名称", charset='utf8' )
    print('数据库连接成功')
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()

    # 使用execute方法执行SQL语句
    cursor.execute("SELECT VERSION()")

    # 使用 fetchone() 方法获取一条数据
    data = cursor.fetchone()

    print(data)

    # 关闭数据库连接
    db.close()
    except pymysql.Error as e:
    print('数据库连接失败:' + str(e))

    三、总结

    Mysqldb 与 pymysql 在写法上有一样的区别



  • 相关阅读:
    Flush the AOS cache from code
    EntityConnectionStringBuilder 构造EF连接字符串
    AX中文转拼音
    AX2012 AOT中Web部署显示二级以上菜单
    clearCompanyCache
    AX2009 打印到PDF优化
    AX ODBC读取其他SQL数据库服务器数据
    AX2009报表打印固定长度Barcode条码
    Create Product Variant
    Rename AOT Object
  • 原文地址:https://www.cnblogs.com/waitingbar/p/15693709.html
Copyright © 2011-2022 走看看