4

使用python3,我可以使用pymysql连接到mysql。一切都按预期工作。随附的代码有效。

import pymysql

conn = pymysql.connect(host='127.0.0.1',  unix_socket='/home/jhgong/mysql/tmp/mysql.sock', user='root', passwd='my_pass', db='my_db', port='3333')
cur = conn.cursor()
cur.execute('select user from auth_users')
for i in cur:
    print(i)

试图让 sqlalchemy 与 pymysql 连接,默认示例字符串似乎不起作用。除非我同时声明端口号和 unix_socket,否则上面的示例不起作用。

下面是我一直用来尝试连接 sqlalchemy 的方法。我假设套接字和端口号都是需要的。我使用 connect_args 来输入带有额外 unix_socket 位置的哈希。没有喜悦。

附上我一直在使用的导致错误的代码段。

conarg = {
   'unix_socket':'/home/jhgong/mysql/tmp/mysql.sock',
   'db'         :'ice'
}
engine = create_engine('mysql+pymysql://root:my_pass@127.0.0.1:3333/my_db',   connect_args = conarg, echo=True)
connection = engine.connect()

无论有没有 conarg 哈希中的 db,我都会收到以下错误:

>>> connection = engine.connect()
2013-01-17 13:04:20,819 INFO sqlalchemy.engine.base.Engine b'SELECT DATABASE()'
2013-01-17 13:04:20,819 INFO sqlalchemy.engine.base.Engine ()
Traceback (most recent call last):
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/pool.py", line 724, in _do_get
    return self._pool.get(wait, self._timeout)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/util/queue.py", line 163, in get
    raise Empty
sqlalchemy.util.queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/engine/base.py", line 1574, in connect 
    return self._connection_cls(self, **kwargs)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/engine/base.py", line 58, in __init__
    self.__connection = connection or engine.raw_connection()
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/engine/base.py", line 1637, in raw_connection
    return self.pool.unique_connection()
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/pool.py", line 182, in unique_connection
    return _ConnectionFairy(self).checkout()
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/pool.py", line 398, in __init__
    rec = self._connection_record = pool._do_get()
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/pool.py", line 744, in _do_get
    con = self._create_connection()
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/pool.py", line 187, in _create_connection 
    return _ConnectionRecord(self)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/pool.py", line 284, in __init__
    exec_once(self.connection, self)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/event.py", line 362, in exec_once
    self(*args, **kw)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/event.py", line 379, in __call__
    fn(*args, **kw)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/engine/strategies.py", line 168, in first_connect
    dialect.initialize(c)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/dialects/mysql/base.py", line 2005, in initialize
    default.DefaultDialect.initialize(self, connection)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/engine/default.py", line 183, in initialize
    self._get_default_schema_name(connection)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/dialects/mysql/base.py", line 1970, in _get_default_schema_name
    return connection.execute('SELECT DATABASE()').scalar()
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/engine/base.py", line 645, in execute
    params)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/engine/base.py", line 791, in _execute_text
    statement, parameters
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/engine/base.py", line 854, in _execute_context
    context)
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/SQLAlchemy-0.8.0b1dev-py3.2.egg/sqlalchemy/engine/default.py", line 342, in do_execute
    cursor.execute(statement, parameters) 
  File "/nfs/site/home/jhgongx/.local/lib/python3.2/site-packages/PyMySQL3-0.5-py3.2.egg/pymysql/cursors.py", line 105, in execute
    query = query % escaped_args
TypeError: unsupported operand type(s) for %: 'bytes' and 'tuple'

它似乎在空池队列上引发错误。设置池队列的大小或类型无效。

关于如何解决这个问题的任何建议?

4

2 回答 2

0

试试这个非常简单的例子:

import sqlalchemy
from sqlalchemy.sql import select
from sqlalchemy import Table, MetaData


def init():
    try:
        server = 'xx'
        db = 'xx'
        login = 'xx'
        passwd = 'xx'
        engine_str = 'mysql+mysqlconnector://{}:{}@{}/{}'.format(login, passwd, server, db)
        engine = sqlalchemy.create_engine(engine_str, echo=False, encoding='utf-8')
        connection = engine.connect()
        metadata = MetaData()
        t_servers = Table('your_table_here', metadata, autoload=True, autoload_with=engine)
        s = select([t_servers])
        result = connection.execute(s)
        for row in result:
            print(row['the_field'])
    except Exception:
        raise
    finally:
        connection.close()
if __name__ == '__main__':
    init()

适用于 Python 3 的 Mysql 连接器:在此处下载

于 2014-03-06T00:23:10.667 回答
0

我知道这已经晚了,但是PyMySQL 要求提到CPython >= 2.6 or >= 3.3了,看起来您使用的是 CPython 3.2(使用的默认 python 版本),这可能是您的问题。

于 2016-01-04T20:40:07.667 回答