我使用mysql.connector
连接到mysql
,这是我的Python句子:
conn = mysql.connector.connect(user = 'root', password = 'password', database = 'test', use_unicode = True)`
但是有一个错误,这是日志:
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()
我在test
中创建了数据库mysql
谢谢
答案 0 :(得分:0)
您使用的是哪个Python版本?
struct是一个内置库,unpack_from方法包含在Python 2.5版中。
如果您使用的是旧版本的Python,请考虑升级到2.7或3.X.
如果您无法转移到较新版本,可以尝试使用较旧版本的连接器(1.0版与Python 2.4兼容)。您可以下载旧版本here