search

Home  >  Q&A  >  body text

Python使用mysqldb连接mysql抛出errorclass errorvalue?

系统Ubuntu16,python3.5,mysql5.7
Python代码

conn = MySQLdb.connect(user="root" , passwd="123" , db="book_database" , charset="utf8")
cur = conn.cursor()
cur.execute("insert into book_db values(%s,%s,%s,%d)",("110","book","sxw",8))
conn.commit()
cur.close()
conn.close()

出错

Traceback (most recent call last):
  File "/home/marshalldteach/PycharmProjects/sxwpython/first.py", line 2, in <module>
    conn = MySQLdb.connect(user="root" , passwd="123" , db="book_database" , charset="utf8")
  File "/usr/local/lib/python3.5/dist-packages/MySQLdb/__init__.py", line 80, in Connect
    from MySQLdb.connections import Connection
  File "/usr/local/lib/python3.5/dist-packages/MySQLdb/connections.py", line 36
    raise errorclass, errorvalue
                    ^
SyntaxError: invalid syntax

数据库格式

desc book_db;
Field Type Null Key Default Extra
book_id char(20) NO PRI NULL
book_name char(20) YES NULL
author char(20) YES NULL
amount int(10) YES NULL

4 rows in set (0.00 sec)

在网上搜索了错误原因,却没有找到类似的问题,想请问一下这里为什么会抛出错误类/错误值,我看了一下,没有发现问题...,希望得到大家的帮助。

大家讲道理大家讲道理2777 days ago1491

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 10:25:10

    python3 no longer supports MySQLdb.
    http://stackoverflow.com/ques...

    reply
    0
  • Cancelreply