我使用sqlalchemy和pandas连接到db并从sql表创建数据帧。
这是我的代码:
port=3306
unix_socket='/var/lib/mysql/mysql.sock'
conn = "mysql://user:pwd@localhost/dbname?port=3306?unix_socket=/var/lib/mysql/mysql.sock"
_engine = create_engine(conn)
print conn
print _engine
db_conn=_engine.connect()
输出:
Traceback (most recent call last):
File "runner_train_test.py", line 37, in <module>
_engine.connect()
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/base. py", line 2018, in connect
return self._connection_cls(self, **kwargs)
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/base. py", line 72, in __init__
if connection is not None else engine.raw_connection()
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/base. py", line 2104, in raw_connection
self.pool.unique_connection, _connection)
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/base. py", line 2074, in _wrap_pool_connect
return fn()
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/pool.py", li ne 318, in unique_connection
return _ConnectionFairy._checkout(self)
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/pool.py", li ne 713, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/pool.py", li ne 480, in checkout
rec = pool._do_get()
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/pool.py", li ne 1060, in _do_get
self._dec_overflow()
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/util/langhel pers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/pool.py", li ne 1057, in _do_get
return self._create_connection()
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/pool.py", li ne 323, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/pool.py", li ne 449, in __init__
self.connection = self.__connect()
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/pool.py", li ne 607, in __connect
connection = self.__pool._invoke_creator(self)
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/strat egies.py", line 97, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/anaconda2/lib/python2.7/site-packages/sqlalchemy/engine/defau lt.py", line 385, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/anaconda2/lib/python2.7/site-packages/MySQLdb/__init__.py", l ine 81, in Connect
return Connection(*args, **kwargs)
File "/usr/local/anaconda2/lib/python2.7/site-packages/MySQLdb/connections.py" , line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required
答案 0 :(得分:0)
您的连接字符串看起来不对。 sqlalchemy documentation说明了以下字符串:mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>
或Google云端的示例:mysql+mysqldb://root@/<dbname>?unix_socket=/cloudsql/<projectid>:<instancename>
因此您的字符串应该可能是:
mysql://user@/dbname?unix_socket=/var/lib/mysql/mysql.sock