mysql AttributeError 'module' 对象没有属性 'unpack_from' [英] mysql AttributeError 'module' object has no attribute 'unpack_from'

查看:59
本文介绍了mysql AttributeError 'module' 对象没有属性 'unpack_from'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用mysql.connector连接mysql,这里是我的Python语句:

I use mysql.connector to connect to mysql, here is my Python sentence:

conn = mysql.connector.connect(user = 'root', password = 'password', database = 'test', use_unicode = True)`

但是有一个错误,这里是日志:

but there is an error, here is the log:

Traceback (most recent call last):
  File "PythonConnectToMySQL.py", line 6, in <module>
    conn = mysql.connector.connect(user = 'root', password = 'hankenqqqq', database = 'test', use_unicode = True)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 162, in connect
    return MySQLConnection(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 129, in __init__
    self.connect(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 454, in connect
    self._open_connection()
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 418, in _open_connection
    self._do_handshake()
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/connection.py", line 141, in _do_handshake
    packet = self._socket.recv()
  File "/usr/local/lib/python2.7/dist-packages/mysql/connector/network.py", line 235, in recv_plain
    payload_len = struct.unpack_from(
AttributeError: 'module' object has no attribute 'unpack_from'`

这是我的代码:

#!/usr/bin/env python2.7
#-*-coding:utf8-*-

import mysql.connector

conn = mysql.connector.connect(user = 'root', password = 'password', database = 'test', use_unicode = True)

cursor = conn.cursor()    
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')

cursor.execute('insert into user(id, name) values (%s, %s)', ['1', 'Mary'])
print cursor.rowcount

conn.commit()
cursor.close()

cursor = conn.cursor()
cursor.execute('select * from user where id = %s', '1')

values = cursor.fetchall()
print values

cursor.close()
conn.close()

我在mysql中创建了数据库test谢谢

I have create database test in mysql Thanks

推荐答案

您使用的是哪个 Python 版本?

Which Python version are you using?

struct 是一个内置库,在 Python 2.5 版本中包含了 unpack_from 方法.

struct is a built-in library, and the unpack_from method was included in Python version 2.5.

如果您使用的是旧版 Python,请考虑升级到 2.7 或 3.X 版.

If you are using an older version of Python consider upgrading to version 2.7 or 3.X.

如果您无法迁移到新版本,您可以尝试使用旧版本的连接器(1.0 版与 Python 2.4 兼容).您可以在此处

If it is not possible for you to move to a newer version, you can try with an older version of the connector (version 1.0 is compatible with Python 2.4). You can download older versions here

这篇关于mysql AttributeError 'module' 对象没有属性 'unpack_from'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆